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
Related Questions on Exception Handling in C plus plus
What is exception handling in C++?
A. A method to handle errors during runtime
B. A method to handle errors during compile time
C. A method to handle errors during linking
D. A method to handle errors during execution
What is the purpose of the 'try' block in exception handling in C++?
A. To catch exceptions
B. To throw exceptions
C. To define the block of code that may generate exceptions
D. To handle exceptions
Which of the following is not a standard exception class in C++?
A. IOException
B. std::exception
C. std::runtime_error
D. std::out_of_range

Join The Discussion