<exception>
Include the standard header <exception>
to define several types and functions related to the handling
of exceptions.
namespace std { class exception; class bad_exception; typedef T1 exception_ptr; [added with C++11] class nested_exception; [added with C++11] // FUNCTIONS typedef void (*terminate_handler)(); typedef void (*unexpected_handler)(); [deprecated with C++11] terminate_handler get_terminate() throw(); [added with C++11] terminate_handler set_terminate(terminate_handler pnew) throw(); unexpected_handler get_unexpected() throw(); [added with C++11] unexpected_handler set_unexpected(unexpected_handler pnew) throw(); [deprecated with C++11] void terminate() noexcept; [does not return] void unexpected(); [does not return] [deprecated with C++11] bool uncaught_exception(); exception_ptr current_exception() noexcept; [added with C++11] void rethrow_exception(exception_ptr ptr); [does not return] [added with C++11] template<class Ex> exception_ptr make_exception_ptr(Ex ex) noexcept; [added with C++11] template<class Ex> void rethrow_if_nested(const Ex& ex); [added with C++11] template<class Ty> void throw_with_nested(Ty&& val); [does not return] [added with C++11] } // namespace std
bad_exception
class bad_exception : public exception { };
The class describes an exception that can be thrown from an
unexpected handler.
The value returned by
what()
is an implementation-defined
C string.
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(); };
The class serves as the base class for all exceptions thrown
by certain expressions and by the Standard C++ library. The
C string 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.
A copy of an exception
returns a C string from what
that compares equal to the original.
None of the member functions throw any exceptions.
current_exception
exception_ptr current_exception() noexcept; [added with C++11]
The function returns an exception pointer that designates the current thrown exception, or a null exception pointer if no such exception exists.
exception_ptr
typedef T1 exception_ptr; [added with C++11]
The typedef is a synonym for the unspecified type T1
,
which describes an
exception pointer that can designate
a current thrown exception, or a null value if no such
exception exists. The thrown exception object has a lifetime
at least as long as that of the exception pointer.
get_terminate
terminate_handler get_terminate() throw(); [added with C++11]
The function returns the stored address of the current terminate handler.
get_unexpected
unexpected_handler get_unexpected() throw(); [added with C++11]
The function returns the stored address of the current unexpected handler.
make_exception_ptr
template<class Ex> exception_ptr make_exception_ptr(Ex ex) noexcept; [added with C++11]
The function returns an
exception pointer that designates
the exception Ex
as a thrown exception.
nested_exception
class nested_exception { public: nested_exception() throw(); nested_exception(const exception&) throw() = default; nested_exception& operator=(const nested_exception& right) throw() = default; virtual ~nested_exception() throw() = default; void rethrow_nested() const; [does not return] exception_ptr nested_ptr() const noexcept; };
The class serves as a wrapper for the current thrown exception
as an object of type
exception_ptr
.
The default constructor stores
current_exception()
which you can retrieve by calling the member function nested_ptr()
or rethrow by calling the member function rethrow_nested()
.
An attempt to rethrow a null nested_ptr()
instead calls
terminate()
.
rethrow_exception
void rethrow_exception(exception_ptr ptr); [does not return] [added with C++11]
The function uses the
exception pointer ptr
to rethrow an earlier thrown exception.
rethrow_exception
never returns to its caller.
rethrow_if_nested
template<class Ex> void rethrow_if_nested(const Ex& ex); [added with C++11]
The function calls ex.rethrow_nested()
if
ex
is publicly derived from
nested_exception
.
Otherwise it does nothing.
set_terminate
terminate_handler set_terminate(terminate_handler pnew) throw();
The function establishes a new
terminate handler
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(); [deprecated with C++11]
The function establishes a new
unexpected handler
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() noexcept; [does not return]
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 must not return to its caller. At
program startup,
the terminate handler is a function that calls
abort()
.
terminate_handler
typedef void (*terminate_handler)();
The type describes a pointer to a function suitable for use as a terminate handler.
throw_with_nested
template<class Ty> void throw_with_nested(Ty&& val); [does not return] [added with C++11]
The function throws
forward<remove_reference<Ty>>(val)
if Ty
is derived from
nested_exception
or is
not suitable for use as a base class. Otherwise it throws an object
that contains both the base class nested_exception
, default
initialized, and the base class Ty
, initialized with
forward<remove_reference<Ty>>(val)
.
throw_with_nested
never returns to its caller.
uncaught_exception
bool uncaught_exception();
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(); [does not return] [deprecated with C++11]
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()
the unexpected handler is the one in effect immediately after evaluating the throw expression.
An unexpected handler must not return to its caller. It may terminate execution by:
bad_exception
terminate()
,
abort()
, or
exit(int)
At program startup,
the unexpected handler is a function that calls
terminate()
.
unexpected_handler
typedef void (*unexpected_handler)();unexpected_handler
The type describes a pointer to a function suitable for use as an unexpected handler.
See also the Table of Contents and the Index.
Copyright © 1992-2013 by P.J. Plauger. All rights reserved.