13.23 <exception>The <exception> header declares classes, types, and functions related to fundamental exception handling. See <stdexcept> for additional exception classes.
A bad_exception object is thrown from the unexpected function when unexpected throws an exception that is not listed in the exception specification that caused unexpected to be called. Most programs do not throw or catch bad_exception. You can list bad_exception in an exception specification if you want to handle this unusual situation differently. See Chapter 5 and unexpected (in this section) for more details. See Alsoterminated function, unexpected function, throw keyword
The exception class is the base class for all exception objects thrown by the standard library or by code generated by the compiler. By convention, user-defined exception classes also derive from exception or from one of its derived classes.
See Alsobad_exception class, bad_alloc in <new>, bad_cast in <typeinfo>, bad_typeid in <typeinfo>, ios_base::failure in <ios>, logic_error in <stdexcept>, runtime_error in <stdexcept>
The set_terminate function saves f to be used by calls to terminate. The previous value of the terminate handler is returned. See Alsoterminate function
The set_unexpected function saves f to be used by calls to unexpected. The previous value of the unexpected handler is returned. See Alsounexpected function
The terminate function is called when normal exception handling cannot handle an exception for any reason—for example, when there is no matching catch block for an exception, or when an exception is thrown and, while the stack is unwinding, another exception is thrown by a destructor. A program might also call terminate explicitly. You can change the behavior of the terminate function by calling set_terminate. The default behavior is to call abort. The terminate function is a last resort because normal exception handling failed. For this reason, you cannot rely on the usual destruction of static objects and objects on the stack. See Alsoset_terminate function, unexpected function, abort function in <cstdlib>, catch keyword
The uncaught_exception function returns true while an exception is processed: after evaluating the argument of a throw expression but before a matching exception declaration is initialized in a catch block. It also returns true after terminate is called (but not for an explicit call to terminate). It returns false at other times. Call uncaught_exception to learn whether exception handling is currently underway. If it returns true, throwing a new exception results in a call to terminate. See Alsoterminate function, catch keyword, throw keyword
If a function has an exception specification and throws an exception that is not listed in the exception specification, the unexpected function is called to handle the unexpected exception. You can implement your own unexpected function. If you do so, you must ensure that unexpected does not return normally. It can terminate the program—for example, by calling terminate—or it can throw an exception. If your unexpected function throws an exception that is not listed in the function's exception specification, a new exception of type bad_exception is created and thrown. If the function's exception specification does not list bad_exception, terminate is called automatically. The default implementation of unexpected calls terminate. In other words, if a function has an exception specification, it is guaranteed that only the specified exceptions can be thrown out of the function, or else the application will be terminated. See Alsobad_exception class, set_unexpected function, abort in <cstdlib>, throw keyword |