C++ Programming Guide

Modifying the terminate() and unexpected() Functions

The following sections describe how to modify the behavior of the terminate() and unexpected() functions using set_terminate() and set_unexpected().

set_terminate()

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 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.


Note -

Selecting a terminate() replacement that returns to its caller, or that does not terminate the program or thread, is an error.


set_unexpected()

You can modify the default behavior of unexpected() by calling the function set_unexpected():


// declarations are in standard header <exception>
namespace std {
   class exception;
   class bad_exception;
   typedef void (*unexpected_handler)();
   unexpected_handler
      set_unexpected(unexpected_handler f) throw();
   void unexpected();
}

The unexpected() function is called when a function attempts to exit via an exception not listed in its exception specification. The default version of unexpected() calls terminate().

A replacement version of unexpected() might throw an exception permitted by the violated exception specification. If it does so, exception handling continues as though the original function had really thrown the replacement exception. If the replacement for unexpected() throws any other exception, that exception is replaced by the standard exception std::bad_exception. If the original function's exception specification does not allow std::bad_exception, function terminate() is called immediately. Otherwise, exception handling continues as though the original function had really thrown std::bad_exception.

unexpected() calls the function passed as an argument to set_unexpected(). Such a function takes no parameters, returns no value, and must not return to its caller. The function passed in the most recent call to set_unexpected() is called. The previous function passed as an argument to set_unexpected() is the return value, so you can implement a stack strategy for using unexpected().


Note -

Selecting an unexpected() replacement that returns to its caller is an error.