13.23 <exception>

The <exception> header declares classes, types, and functions related to fundamental exception handling. See <stdexcept> for additional exception classes.

This section describes a complicated system of exceptions, function calls, and handlers. The end result is simpler than it might seem at first:

A function can throw only the exception types listed in its exception specification, or else the program terminates immediately.

If a function does not have an exception specification, it can throw any exception. (Virtual functions work a little differently; see Chapter 6 for details.)

bad_exception class Wrong exception type

class bad_exception : public exception {


  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 5 and unexpected (in this section) for more details.

See Also

terminated function, unexpected function, throw keyword

exception class Base class for all standard exceptions

class exception {


  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 generated by the compiler. By convention, user-defined exception classes also derive from exception or from one of its derived classes.

figs/acorn.gifvirtual const char* what( ) const throw( );

Returns a message that describes the nature of the exception. The exact contents of the string are implementation-defined; it might be a multibyte string, which can be converted to a wstring.

See Also

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

set_terminate function Changes the terminate( ) handler

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.

See Also

terminate function

set_unexpected function Changes the unexpected( ) handler

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.

See Also

unexpected function

terminate function Terminates program when exception handling fails

void terminate(  )

The terminate function is called when normal exception handling cannot handle an exception for any reasonfor 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 Also

set_terminate function, unexpected function, abort function in <cstdlib>, catch keyword

uncaught_exception function Determines whether exception handling is currently underway

bool uncaught_exception(  )

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 Also

terminate function, catch keyword, throw keyword

unexpected function Handles an unexpected exception type

void unexpected(  )

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 programfor example, by calling terminateor 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 Also

bad_exception class, set_unexpected function, abort in <cstdlib>, throw keyword