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 func1()
{
B b;
throw b;
}
void func2()
{
A a;
throw a;
}
int main()
{
try{
func1();
}
catch(...){
cout<<"Caught All types of exceptions\n";
}
try{
func2();
}
catch(B b){
cout<<"Caught All types of exceptions\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 func1()
{
B b;
throw b;
}
void func2()
{
A a;
throw a;
}
int main()
{
try{
func1();
}
catch(...){
cout<<"Caught All types of exceptions\n";
}
try{
func2();
}
catch(B b){
cout<<"Caught All types of exceptions\n";
}
}
A. Caught All types of exceptions
B. Caught All types of exceptions
Aborted (core dumped)
C. Error
D. Caught All types of exceptions
Caught All types of exceptions
Answer: Option B
Join The Discussion