# <exception>
<exception>
# ``
* * *
Include the standard header **``**
to define several types and functions related to the handling
of exceptions.
namespace std {
class exception;
class bad_exception;
// FUNCTIONS
typedef void (*terminate_handler)();
typedef void (*unexpected_handler)();
terminate_handler
set_terminate(terminate_handler pnew) throw();
unexpected_handler
set_unexpected(unexpected_handler pnew) throw();
void terminate();
void unexpected();
bool uncaught_exception();
} // namespace stdCopy to clipboard
## `bad_exception`
class bad_exception
: public exception {
};Copy to clipboard
The class describes an exception that can be thrown from an
[unexpected handler](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_exceptio.html#unexpected%20handler).
The value returned by
`what()`
is an implementation-defined
[C string](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_over.html#C%20string).
None of the member functions throw any exceptions.
## `exception`
class exception {
public:
exception() throw();
exception(const exception& right) throw();
exception& operator=(const exception& right) throw();
virtual ~exception() throw();
virtual const char *what() const throw();
};Copy to clipboard
The class serves as the base class for all exceptions thrown
by certain expressions and by the Standard C++ library. The
[C string](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_over.html#C%20string) value returned by
**`what()`**
is left unspecified by the default constructor,
but may be defined by the constructors for certain derived classes
as an implementation-defined
[C string](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_over.html#C%20string).
None of the member functions throw any exceptions.
## `set_terminate`
terminate_handler
set_terminate(terminate_handler pnew) throw();Copy to clipboard
The function establishes a new
[terminate handler](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_exceptio.html#terminate%20handler)
as the function `*pnew`. Thus, `pnew` must
not be a null pointer. The function returns the address of the
previous terminate handler.
## `set_unexpected`
unexpected_handler
set_unexpected(unexpected_handler pnew) throw();Copy to clipboard
The function establishes a new
[unexpected handler](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_exceptio.html#unexpected%20handler)
as the function `*pnew`. Thus, `pnew` must
not be a null pointer. The function returns the address of the
previous unexpected handler.
## `terminate`
void terminate();Copy to clipboard
The function calls a
**terminate handler**,
a function of type `void ()`.
If `terminate` is called directly by the program,
the terminate handler is the one most recently set by a call to
`set_terminate`.
If `terminate` is called for any of several other
reasons during evaluation of a throw expression,
the terminate handler is the one in effect immediately after
evaluating the throw expression.
A terminate handler may not return to its caller. At
[program startup](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_over.html#program%20startup),
the terminate handler is a function that calls
`abort()`.
## `terminate_handler`
typedef void (*terminate_handler)();Copy to clipboard
The type describes a pointer to a function suitable for use as a
[terminate handler](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_exceptio.html#terminate%20handler).
## `uncaught_exception`
bool uncaught_exception();Copy to clipboard
The function returns true only if a thrown exception is being currently
processed. Specifically, it returns true after completing evaluation of a
throw expression and before completing initialization of the exception
declaration in the matching handler or calling
`unexpected` as a result of the
throw expression.
## `unexpected`
void unexpected();Copy to clipboard
The function calls an
**unexpected handler**,
a function of type `void ()`.
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 func() throw() // function may throw no exceptions
{throw "bad"; } // throw calls unexpected()Copy to clipboard
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`](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_exceptio.html#bad_exception)
- calling
`terminate()`,
`abort()`, or
`exit(int)`
At [program startup](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_over.html#program%20startup),
the unexpected handler is a function that calls
`terminate()`.
## `unexpected_handler`
typedef void (*unexpected_handler)();Copy to clipboard
The type describes a pointer to a function suitable for use as an
[unexpected handler](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_exceptio.html#unexpected%20handler).
* * *
See also the
**[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#Table%20of%20Contents)** and the
**[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_cpp_index.html)**.
*[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_crit_pjp.html) © 1992-2006by P.J. Plauger. All rights reserved.*
Last Published: Oct 06, 2026
Previous Topic
Next Topic