Namespaces
Variants
Views
Actions

Throwing exceptions

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
throw-expression
try block
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 
Exceptions
try block
Throwing exceptions
Handling exceptions
Exception specification
    noexcept specification (C++11)
    dynamic specification (until C++17*)
noexcept operator (C++11)
 

Throwing an exception transfers control to a handler.

An exception can be thrown from throw expressions, the following contexts may also throw exceptions:

Contents

[edit] Exception object

Throwing an exception initializes an object with dynamic storage duration, called the exception object.

If the type of the exception object would be one of the following types, the program is ill-formed:

[edit] Constructing and destructing exception objects

Given the type of the exception object as T:

  • Let obj be an lvalue of type const T, the copy-initialization of an object of type T from obj must be well-formed.
  • If T is a class type: