The following example shows the flow of control and special functions used in exception handling:
#include <iostream>
#include <exception>
using namespace std;
class X { };
class Y { };
class A { };
// pfv type is pointer to function returning void
typedef void (*pfv)();
void my_terminate() {
cout << "Call to my terminate" << endl;
abort();
}
void my_unexpected() {
cout << "Call to my_unexpected()" << endl;
throw;
}
void f() throw(X,Y, bad_exception) {
throw A();
}
void g() throw(X,Y) {
throw A();
}
int main()
{
pfv old_term = set_terminate(my_terminate);
pfv old_unex = set_unexpected(my_unexpected);
try {
cout << "In first try block" << endl;
f();
}
catch(X) {
cout << "Caught X" << endl;
}
catch(Y) {
cout << "Caught Y" << endl;
}
catch (bad_exception& e1) {
cout << "Caught bad_exception" << endl;
}
catch (...) {
cout << "Caught some exception" << endl;
}
cout << endl;
try {
cout << "In second try block" << endl;
g();
}
catch(X) {
cout << "Caught X" << endl;
}
catch(Y) {
cout << "Caught Y" << endl;
}
catch (bad_exception& e2) {
cout << "Caught bad_exception" << endl;
}
catch (...) {
cout << "Caught some exception" << endl;
}
}
The following is the output of the above example:
In first try block
Call to my_unexpected()
Caught bad_exception
In second try block
Call to my_unexpected()
Call to my terminate
At run time, this program behaves as follows:
Note that the catch blocks following the second try block are not entered, because the exception was handled by my_unexpected() as an unexpected throw, not as a valid exception.