You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exception handling provides a way of transferring control and information from some point in the execution of a program to a handler associated with a point previously passed by the execution (in other words, exception handling transfers control up the call stack).
15
15
16
-
Evaluating a <DocLinkdest="cpp/language/throw.html#throw_expressions">`throw` expression</DocLink> will throw an exception. Exceptions can also be thrown in <DocLinkdest="cpp/language/throw.html#throw_expressions">other contexts</DocLink>.
16
+
Evaluating a <DocLinkdest="cpp/language/exceptions/throw.html#throw_expressions">`throw` expression</DocLink> will throw an exception. Exceptions can also be thrown in <DocLinkdest="cpp/language/exceptions/throw.html#throw_expressions">other contexts</DocLink>.
17
17
18
-
In order for an exception to be caught, the `throw` expression has to be inside a <DocLinkdest="cpp/language/try">`try` block</DocLink>, and the `try` block has to contain a <DocLinkdest="cpp/language/catch">handler</DocLink> that matches the type of the exception object.
18
+
In order for an exception to be caught, the `throw` expression has to be inside a <DocLinkdest="cpp/language/exceptions/try">`try` block</DocLink>, and the `try` block has to contain a <DocLinkdest="cpp/language/exceptions/catch">handler</DocLink> that matches the type of the exception object.
19
19
20
20
When declaring a function, the following specification(s) may be provided to limit the types of the exceptions a function may throw:
Errors that arise during exception handling are handled by <DocLinkdest="cpp/error/terminate">`std::terminate`</DocLink> and <Revisionsince="C++17"><DocLinkdest="cpp/error/unexpected">`std::unexpected`</DocLink></Revision>.
An <DocLinkdest="/cpp/language/exceptions">exception</DocLink> can be handled by a handler.
14
14
15
-
###Handler
15
+
## Handler
16
16
17
17
<DeclDocid={1}>
18
18
<Declslot="decl">
@@ -71,7 +71,7 @@ If the parameter is declared to have type “array of `T`” or function type `T
71
71
72
72
A handler with parameter type `T` can be abbreviated as “a handler of type `T`”.
73
73
74
-
###Matching exceptions
74
+
## Matching exceptions
75
75
76
76
Each `try` block associates with a number of handlers, these handlers form a handler sequence. When an exception is thrown from a `try` block, the handlers in the sequence are tried in order of appearance to match the exception.
77
77
@@ -106,11 +106,11 @@ If no match is found among the handlers for a `try` block, the search for a matc
106
106
107
107
If no matching handler is found, <DocLinkdest="/cpp/error/terminate">std::terminate</DocLink> is invoked; whether or not the stack is <DocLinkdest="/cpp/language/exceptions/throwing_exceptions#Stack_unwinding">unwound</DocLink> before this invocation of <DocLinkdest="/cpp/error/terminate">std::terminate</DocLink> is implementation-defined.
108
108
109
-
###Handling exceptions
109
+
## Handling exceptions
110
110
111
111
When an exception is thrown, control is transferred to the nearest handler with a matching type; “nearest” means the handler for which the compound statement or the member initializer list (if present) following the `try` keyword was most recently entered by the thread of control and not yet exited.
112
112
113
-
####Initializing the handler parameter
113
+
### Initializing the handler parameter
114
114
115
115
The parameter declared in the parameter list (if any), of type “possibly cv-qualified `T`” or “lvalue reference to possibly cv-qualified `T`”, is initialized from the <DocLinkdest="/cpp/language/exceptions/throwing_exceptions#Exception_object">exception object</DocLink>, of type `E`, as follows:
116
116
@@ -123,7 +123,7 @@ When the parameter is declared as an object, any changes to that object will not
123
123
124
124
When the parameter is declared as a reference to an object, any changes to the referenced object are changes to the exception object and will have effect should that object be rethrown.
125
125
126
-
####Activating the handler
126
+
### Activating the handler
127
127
128
128
A handler is considered _active_ when initialization is complete for the parameter (if any) of the handler.
129
129
@@ -133,7 +133,7 @@ A handler is no longer considered active when the handler exits.
133
133
134
134
The exception with the most recently activated handler that is still active is called the _currently handled exception_. Such an exception can be <DocLinkdest="/cpp/language/exceptions/throwing_exceptions#throw_expressions">rethrown</DocLink>.
135
135
136
-
###Control flow
136
+
## Control flow
137
137
138
138
The `compound-statement` of a handler is a <DocLinkdest="/cpp/language/statements#Control-flow-limited_statements">control-flow-limited statement</DocLink>:
139
139
@@ -153,7 +153,7 @@ void f()
153
153
}
154
154
```
155
155
156
-
###Notes
156
+
## Notes
157
157
158
158
<DocLinkdest="/cpp/language/exceptions/throwing_exceptions#Stack_unwinding">Stack unwinding</DocLink> occurs while control is transferring to a handler. When a handler becomes active, stack unwinding is already completed.
Copy file name to clipboardExpand all lines: src/content/docs/cpp/language/exceptions/noexcept.mdx
+25-23Lines changed: 25 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,13 +22,13 @@ It can be used within a function template's <DocLink dest="/cpp/language/excepti
22
22
noexcept(/*$s:expression*/)
23
23
```
24
24
</Decl>
25
-
</DeclDoc>
26
25
27
-
Returns a <DocLink dest="/cpp/language/value_category#prvalue">prvalue</DocLink> of type `bool`. The result is `true` if <Revision until="C++17">the set of <DocLink dest="/cpp/language/exceptions/exception_specification/dynamic_specification" text="potential exceptions" /> of the _expression_ is empty</Revision><Revision since="C++17">_expression_ is specified to be <DocLink dest="/cpp/language/exceptions/exception_specification/noexcept_specification" text="non-throwing" /></Revision>, and `false` otherwise.
26
+
Returns a <DocLink dest="/cpp/language/value_category#prvalue">prvalue</DocLink> of type `bool`. The result is `true` if <Revision until="C++17">the set of <DocLink dest="/cpp/language/exceptions/exception_specification/dynamic_specification" text="potential exceptions" /> of the _expression_ is empty</Revision><Revision since="C++17">_expression_ is specified to be <DocLink dest="/cpp/language/exceptions/exception_specification/noexcept_specification">non-throwing</DocLink></Revision>, and `false` otherwise.
28
27
29
-
_expression_ is an <DocLink dest="/cpp/language/expressions#Potentially-evaluated_expressions">unevaluated operand</DocLink>.
28
+
_expression_ is an <DocLink dest="/cpp/language/expressions#Potentially-evaluated_expressions">unevaluated operand</DocLink>.
30
29
31
-
<Revision since="C++17">If _expression_ is a prvalue, <DocLink dest="/cpp/language/implicit_cast#Temporary_materialization">temporary materialization</DocLink> is applied.</Revision>
30
+
<Revision since="C++17">If _expression_ is a prvalue, <DocLink dest="/cpp/language/implicit_cast#Temporary_materialization">temporary materialization</DocLink> is applied.</Revision>
31
+
</DeclDoc>
32
32
33
33
## Notes
34
34
@@ -40,7 +40,7 @@ If _expression_ is of a class type or (possibly multidimensional) array thereof,
Throwing an <DocLinkdest="cpp/language/exceptions">exception</DocLink> transfers control to a <DocLinkdest="cpp/language/catch">handler</DocLink>.
14
14
15
-
An exception can be thrown from <DocLinkdest="cpp/language/throw#throw_expressions">`throw` expressions</DocLink>, the following contexts may also throw exceptions:
15
+
An exception can be thrown from <DocLinkdest="cpp/language/exceptions/throw#throw_expressions">`throw` expressions</DocLink>, the following contexts may also throw exceptions:
Throwing an exception initializes an object with dynamic <DocLinkdest="cpp/language/storage_duration">storage duration</DocLink>, called the *exception object*.
26
26
@@ -30,7 +30,7 @@ If the type of the exception object would be one of the following types, the pro
30
30
- an <DocLinkdest="cpp/language/abstract_class">abstract class type</DocLink>
31
31
- a pointer to an incomplete type other than (possibly cv-qualified) `void`
32
32
33
-
####Constructing and destructing exception objects
33
+
### Constructing and destructing exception objects
34
34
35
35
Given the type of the exception object as `T`:
36
36
@@ -41,7 +41,7 @@ Given the type of the exception object as `T`:
41
41
42
42
The memory for the exception object is allocated in an unspecified way. The only guarantee is that the storage will never be allocated by global <DocLinkdest="cpp/memory/new/operator_new">allocation functions</DocLink>.
43
43
44
-
If a <DocLinkdest="cpp/language/catch">handler</DocLink> exits by <DocLinkdest="cpp/language/throw#throw_expressions">rethrowing</DocLink>, control is passed to another handler for the same exception object. The exception object is not destructed in this case.
44
+
If a <DocLinkdest="cpp/language/catch">handler</DocLink> exits by <DocLinkdest="cpp/language/exceptions/throw#throw_expressions">rethrowing</DocLink>, control is passed to another handler for the same exception object. The exception object is not destructed in this case.
45
45
46
46
<RevisionBlockuntil="C++11">
47
47
When the last remaining active handler for the exception exits by any means other than rethrowing, the exception object is destroyed and the implementation may deallocate the memory for the temporary object in an unspecified way.
@@ -57,7 +57,7 @@ The points of potential destruction for the exception object are:
57
57
Among all points of potential destruction for the exception object, there is an unspecified last one where the exception object is destroyed. All other points <DocLinkdest="cpp/language/multithread">happen before</DocLink> that last one. The implementation may then deallocate the memory for the exception object in an unspecified way.
58
58
</RevisionBlock>
59
59
60
-
###`throw` expressions
60
+
## `throw` expressions
61
61
62
62
<DeclDocid={1}>
63
63
<Declslot="decl">
@@ -104,7 +104,7 @@ catch (...) // catch all exceptions
104
104
}
105
105
```
106
106
107
-
###Stack unwinding
107
+
## Stack unwinding
108
108
109
109
Once the exception object is constructed, the control flow works backwards (up the call stack) until it reaches the start of a <DocLinkdest="cpp/language/try">`try` block</DocLink>, at which point the parameters of all associated handlers are compared, in order of appearance, with the type of the exception object to find a <DocLinkdest="cpp/language/catch#Matching_exceptions">match</DocLink>. If no match is found, the control flow continues to unwind the stack until the next `try` block, and so on. If a match is found, the control flow jumps to the matching handler.
110
110
@@ -124,7 +124,7 @@ If any function that is called directly by the stack unwinding mechanism, after
124
124
125
125
If an exception is thrown and not caught, including exceptions that escape the initial function of <DocLinkdest="cpp/thread/thread">`std::thread`</DocLink>, the main function, and the constructor or destructor of any static or thread-local objects, then <DocLinkdest="cpp/error/terminate">`std::terminate`</DocLink> is called. It is implementation-defined whether any stack unwinding takes place for uncaught exceptions.
126
126
127
-
###Notes
127
+
## Notes
128
128
129
129
When rethrowing exceptions, the second form must be used to avoid object slicing in the (typical) case where exception objects use inheritance:
0 commit comments