For example in the following code:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A::A()" << endl; }
~A() { cout << "A::~A()" << endl; throw "A::exception"; }
};
class B {
public:
B() { cout << "B::B()" << endl; throw "B::exception"; }
~B() { cout << "B::~B()"; }
};
int main(int, char**)
{
try {
cout << "Entering try...catch block" << endl;
A objectA;
B objectB;
cout << "Exiting try...catch block" << endl;
}
catch (char* ex) {
cout << ex << endl;
}
return 0;
}
B
's destructor throws an exception, which invokes A
's destructor while unwinding the stack, resulting in the throw of another exception.
What will be the program's reaction?