/************************************************************************ *
The following example shows the flow of control and special
functions used in exception handling:
* ************************************************************************/
#include <terminat.h> #include <unexpect.h> #include <iostream.h> 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; } void my_unexpected() { cout << "Call to my unexpected" << endl; } void f() throw(X,Y) // f() is permitted to throw objects of class // types X and Y only { A aobj; throw(aobj); // error, f() throws a class A object } main() { pfv old_term = set_terminate(my_terminate); pfv old_unex = set_unexpected(my_unexpected); try{ f(); } catch(X) { /* ... */ } catch(Y) { /* ... */ } catch (...) { /* ... */ } set_unexpected(old_unex); try { f();} catch(X) { /* ... */ } catch(Y) { /* ... */ } catch (...) { /* ... */ } }
/************************************************************************ *
At run time, this program behaves as follows:
At run time, the following information is displayed, and the
program ends:
Call to my_unexpected Call to my_terminate
Note: The catch blocks following the try block are not entered
, because the exception was handled by my_unexpected() as an
unexpected throw, not as a valid exception.
* ************************************************************************/