C++ Programming Guide

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.