What will be the output of the following C++ code? [code] #include <iostream> #include <string> #include <cstdlib> using namespace std; class

Daniel William

New member
What will be the output of the following C++ code?
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(B *b){
		cout<<"Caught B Class\n";
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
}
A. Caught B Class
B. Caught A Class
C. Compile-time error
D. Run-time error
 
Answer: b
Explanation: The func() throws the object of class B but as catch block is defined to catch the exception of class B, Therefore the exception is caught by the base class A. The programmer has defined the catch block for B*, therefore, the object B is not caught by the pointer object B*.
 

Tìm kiếm

Back
Top