Processing Unexpected Exceptions

Function unexpected calls the function registered with function set_unexpected (defined in header file ). If no function has been registered in this manner, function terminate is called by default. Cases in which function terminate is called include:

  1. the exception mechanism cannot find a matching catch for a thrown exception
  2. a destructor attempts to tHRow an exception during stack unwinding
  3. an attempt is made to rethrow an exception when there is no exception currently being handled
  4. a call to function unexpected defaults to calling function terminate

(Section 15.5.1 of the C++ Standard Document discusses several additional cases.) Function set_terminate can specify the function to invoke when terminate is called. Otherwise, terminate calls abort, which terminates the program without calling the destructors of any remaining objects of automatic or static storage class. This could lead to resource leaks when a program terminates prematurely.

Function set_terminate and function set_unexpected each return a pointer to the last function called by terminate and unexpected, respectively (0, the first time each is called). This enables the programmer to save the function pointer so it can be restored later. Functions set_terminate and set_unexpected take as arguments pointers to functions with void return types and no arguments.

If the last action of a programmer-defined termination function is not to exit a program, function abort will be called to end program execution after the other statements of the programmer-defined termination function are executed.

Категории