What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
cout<<"Caught A Class\n";
}
catch(B b){
cout<<"Caught B Class\n";
}
}
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
cout<<"Caught A Class\n";
}
catch(B b){
cout<<"Caught B Class\n";
}
}
A. Caught B Class
B. Caught A Class
C. Compile-time error
D. Run-time error
Answer: Option B
Join The Discussion