set_unexpected() and set_terminate() Functions

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(). set_unexpected() and set_terminate() are included in the standard header files <unexpect.h>. and <terminat.h>. 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 final action of that program should be to exit from the program. If you attempt to return from the function called by terminate(), abort () is called instead and the program ends.

Note: Providing a call to longjmp() inside a user-defined terminate function can transfer execution control to some other desired point. When you call longjmp, objects existing at the time of a setjmp call will still exist, but some objects constructed after the call to setjmp might not be destructed.



C++ Exception Handling Overview


Example of Using the Exception Handling Functions


unexpected() and terminate() Functions
Using Exception Handling
Transferring Control