The <exception>
header declares classes, types, and functions related to fundamental exception handling.
class bad_exception : public exception { public: bad_exception() throw(); bad_exception(const bad_exception&) throw(); bad_exception& operator=(const bad_exception&) throw(); virtual const char* what() const throw(); };
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 6, Functions, and unexpected
(in this section) for more details.
terminated function, throw keyword, unexpected function
class exception { public: exception() throw(); exception(const exception&) throw(); exception& operator=(const exception&) throw(); virtual ~exception() throw(); virtual const char* what() const throw(); };
The exception
class is the base class for all exception objects thrown by the standard library or by code genererated by the compiler. By convention, user-defined exception classes also derive from exception
or from one of its derived classes.
virtual const char* what() const throw();
what()
function returns a message that describes the nature of the exception. The exact contents of the string is implementation-defined; it might be a multi-byte string, which can be converted to a wstring
.bad_alloc class, bad_cast class, bad_exception class, bad_typeid class, failure class, logic_error class, runtime_error class, <stdexcept>
typedef void (*terminate_handler)(); terminate_handler set_terminate(terminate_handler f) throw();
The set_terminate()
function saves f to be used by calls to terminate()
. The previous value of the terminate handler is returned.
terminate function
typedef void (*unexpected_handler)(); unexpected_handler set_unexpected(unexpected_handler f) throw();
The set_unexpected()
function saves f to be used by calls to unexpected()
. The previous value of the unexpected handler is returned.
unexpected function
void terminate();
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 raised, and while the stack is unwinding, another exception is raised 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 that reason, you cannot rely on the usual destruction of static objects and objects on the stack.
abort function, catch keyword, set_terminate function, unexpected function
bool uncaught_exception();
The uncaught_exception()
function returns true during processing of an exception: 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 ongoing. If it returns true, throwing a new exception results in a call to terminate()
.
catch keyword, terminate function, throw keyword
void unexpected();
If a function has an exception specification, and it 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, say, by calling terminate()
, or it can throw an exception.
If the unexpected()
function throws an exception that is not listed in the function's exception specification, a new exception is created and thrown, of type bad_exception
. 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, or else the application will be terminated.
abort function, bad_exception class, set_terminate function, throw keyword