unexpected() — Handle exception not listed in exception specification

Standards

Standards / Extensions C or C++ Dependencies
ANSI/ISO
C++
C++ only  

Format

#include <exception>

void unexpected(void);

General description

The unexpected() function is part of the z/OS® XL C++ error handling mechanism. If unexpected() is called directly by the program, the unexpected_handler is the one most recently set by a call to set_unexpected(). If unexpected() is called when control leaves a function by a thrown exception of a type not permitted by an exception specification for the function, as in:
void f() throw()      // function may throw no exceptions
    {throw "bad"; }   // throw calls unexpected()
the unexpected_handler is the one in effect immediately after evaluating the throw expression.
An unexpected_handler may not return to its caller. It may terminate execution by:
  • Throwing an object of a type listed in the exception specification (or an object of any type if the unexpected handler is called directly by the program).
  • Throwing an object of type bad_exception.
  • Calling terminate(), abort(), or exit(int).
If set_unexpected() has not yet been called, then unexpected() calls terminate().

In a multithreaded environment, if a thread throws an exception that is not listed in its exception specification, then unexpected() is called. The default for unexpected() is to call terminate(), which defaults to calling abort(), which then causes a SIGABRT signal to be generated to the thread issuing the throw. If the SIGABRT signal is not caught, the process is terminated. You can replace the default unexpected() behavior for all threads in the process by using the set_unexpected() function. One possible use of set_unexpected() is to call a function which issues a pthread_exit(). If this is done, a throw of a condition by a thread that is not in the exception specification results in thread termination, but not process termination.

Returned value

unexpected() returns no values.

Refer to z/OS XL C/C++ Language Reference for more information about z/OS XL C++ exception handling, including the unexpected() function.

Related information