If a matching handler (or ellipsis catch handler) cannot be found for the current exception, the predefined terminate
function is called. (You can also explicitly call terminate
in any of your handlers.) The default action of terminate
is to call abort
. If you want terminate
to call some other function in your program before exiting the application, call the set_terminate
function with the name of the function to be called as its single argument. You can call set_terminate
at any point in your program. The terminate
routine always calls the last function given as an argument to set_terminate
. For example:
#include <eh.h> // For function prototypes
//...
void term_func() { // ... }
int main()
{
try
{
// ...
set_terminate( term_func );
// ...
throw "Out of memory!"; // No catch handler for this exception
}
catch( int )
{
cout << "Integer exception raised.";
}
return 0;
}
The term_func
function should terminate the program or current thread, ideally by calling exit
. If it doesn’t, and instead returns to its caller, abort
is called.
For more information about C++ exception handling, see the C++ Annotated Reference Manual by Margaret Ellis and Bjarne Stroustrup.