You can modify the default behavior of terminate() by calling the function set_terminate(), as shown in the following example.
// declarations are in standard header <exception> namespace std { typedef void (*terminate_handler)(); terminate_handler set_terminate(terminate_handler f) throw(); void terminate(); }
The terminate() function is called in any of the following circumstances:
The exception handling mechanism calls a user function (including destructors for automatic objects) that exits via an uncaught exception while another exception remains uncaught.
The exception handling mechanism cannot find a handler for a thrown exception.
The construction or destruction of a nonlocal object with static storage duration exits using an exception.
Execution of a function registered with atexit() exits using an exception.
A throw expression with no operand attempts to rethrow an exception and no exception is being handled.
The unexpected() function throws an exception that is not allowed by the previously violated exception specification, and std::bad_exception is not included in that exception specification.
The default version of unexpected() is called.
The terminate() function calls the function passed as an argument to set_terminate(). Such a function takes no parameters, returns no value, and must terminate the program (or the current thread). The function passed in the most recent call to set_terminate() is called. The previous function passed as an argument to set_terminate() is the return value, so you can implement a stack strategy for using terminate(). The default function for terminate() calls abort() for the main thread and thr_exit() for other threads. Note that thr_exit() does not unwind the stack or call C++ destructors for automatic objects.
Selecting a terminate() replacement that returns to its caller, or that does not terminate the program or thread, is an error.