The set_unexpected() and set_terminate() functions (C++ only)

The function unexpected(), when invoked, calls the function most recently supplied as an argument to set_unexpected(). If set_unexpected() has not yet been called, unexpected() calls terminate().

The function terminate(), when invoked, calls the function most recently supplied as an argument to set_terminate(). If set_terminate() has not yet been called, terminate() calls abort(), which ends the program.

You can use set_unexpected() and set_terminate() to register functions you define to be called by unexpected() and terminate(). The functions set_unexpected() and set_terminate() are included in the standard header files. Each of these functions has as its return type and its argument type a pointer to function with a void return type and no arguments. The pointer to function you supply as the argument becomes the function called by the corresponding special function: the argument to set_unexpected() becomes the function called by unexpected(), and the argument to set_terminate() becomes the function called by terminate().

Both set_unexpected() and set_terminate() return a pointer to the function that was previously called by their respective special functions (unexpected() and terminate()). By saving the return values, you can restore the original special functions later so that unexpected() and terminate() will once again call terminate() and abort().

If you use set_terminate() to register your own function, the function should no return to its caller but terminate execution of the program.

If you attempt to return from the function called by terminate(), abort() is called instead and the program ends.