- tmp/tmptkgpth1u/{from.md → to.md} +137 -96
tmp/tmptkgpth1u/{from.md → to.md}
RENAMED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
# Exception handling <a id="except">[[except]]</a>
|
| 2 |
|
| 3 |
Exception handling provides a way of transferring control and
|
| 4 |
information from a point in the execution of a thread to an exception
|
| 5 |
handler associated with a point previously passed by the execution. A
|
| 6 |
-
handler will be invoked only by
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
``` bnf
|
| 11 |
try-block:
|
| 12 |
'try' compound-statement handler-seq
|
| 13 |
```
|
|
@@ -38,18 +38,15 @@ exception-declaration:
|
|
| 38 |
throw-expression:
|
| 39 |
'throw' assignment-expressionₒₚₜ
|
| 40 |
```
|
| 41 |
|
| 42 |
The optional *attribute-specifier-seq* in an *exception-declaration*
|
| 43 |
-
appertains to the
|
| 44 |
-
[[except.handle]]).
|
| 45 |
|
| 46 |
A *try-block* is a *statement* (Clause [[stmt.stmt]]). A
|
| 47 |
-
*throw-expression* is of type `void`.
|
| 48 |
-
|
| 49 |
-
subsequently gets control is called a “handler.” Within this Clause “try
|
| 50 |
-
block” is taken to mean both *try-block* and *function-try-block*.
|
| 51 |
|
| 52 |
A `goto` or `switch` statement shall not be used to transfer control
|
| 53 |
into a try block or into a handler.
|
| 54 |
|
| 55 |
``` cpp
|
|
@@ -117,13 +114,17 @@ catch (...) {
|
|
| 117 |
}
|
| 118 |
```
|
| 119 |
|
| 120 |
## Throwing an exception <a id="except.throw">[[except.throw]]</a>
|
| 121 |
|
| 122 |
-
Throwing an exception transfers control to a handler. An
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
``` cpp
|
| 127 |
throw "Help!";
|
| 128 |
```
|
| 129 |
|
|
@@ -165,23 +166,21 @@ When an exception is thrown, control is transferred to the nearest
|
|
| 165 |
handler with a matching type ([[except.handle]]); “nearest” means the
|
| 166 |
handler for which the *compound-statement* or *ctor-initializer*
|
| 167 |
following the `try` keyword was most recently entered by the thread of
|
| 168 |
control and not yet exited.
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
type of the exception object
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
treated exactly as a function argument in a call ([[expr.call]]) or the
|
| 182 |
-
operand of a return statement.
|
| 183 |
|
| 184 |
The memory for the exception object is allocated in an unspecified way,
|
| 185 |
except as noted in [[basic.stc.dynamic.allocation]]. If a handler exits
|
| 186 |
by rethrowing, control is passed to another handler for the same
|
| 187 |
exception. The exception object is destroyed after either the last
|
|
@@ -192,43 +191,48 @@ whichever is later. In the former case, the destruction occurs when the
|
|
| 192 |
handler exits, immediately after the destruction of the object declared
|
| 193 |
in the *exception-declaration* in the handler, if any. In the latter
|
| 194 |
case, the destruction occurs before the destructor of
|
| 195 |
`std::exception_ptr` returns. The implementation may then deallocate the
|
| 196 |
memory for the exception object; any such deallocation is done in an
|
| 197 |
-
unspecified way.
|
| 198 |
-
|
| 199 |
-
|
| 200 |
|
| 201 |
-
When the thrown object is a class object, the
|
| 202 |
-
the destructor shall be accessible, even if
|
| 203 |
-
elided ([[class.copy]]).
|
| 204 |
|
| 205 |
An exception is considered caught when a handler for that exception
|
| 206 |
becomes active ([[except.handle]]). An exception can have active
|
| 207 |
handlers and still be considered uncaught if it is rethrown.
|
| 208 |
|
| 209 |
-
If the exception handling mechanism, after completing
|
| 210 |
-
|
| 211 |
-
function that exits via an exception,
|
| 212 |
-
[[except.terminate]]).
|
| 213 |
|
| 214 |
``` cpp
|
| 215 |
struct C {
|
| 216 |
C() { }
|
| 217 |
-
C(const C&) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
};
|
| 219 |
|
| 220 |
int main() {
|
| 221 |
try {
|
| 222 |
-
throw C(); // calls std::terminate()
|
|
|
|
| 223 |
} catch(C) { }
|
| 224 |
}
|
| 225 |
```
|
| 226 |
|
| 227 |
A *throw-expression* with no operand rethrows the currently handled
|
| 228 |
exception ([[except.handle]]). The exception is reactivated with the
|
| 229 |
-
existing
|
| 230 |
exception is no longer considered to be caught; therefore, the value of
|
| 231 |
`std::uncaught_exception()` will again be `true`. code that must be
|
| 232 |
executed because of an exception yet cannot completely handle the
|
| 233 |
exception can be written like this:
|
| 234 |
|
|
@@ -246,14 +250,14 @@ If no exception is presently being handled, executing a
|
|
| 246 |
*throw-expression* with no operand calls `std::terminate()` (
|
| 247 |
[[except.terminate]]).
|
| 248 |
|
| 249 |
## Constructors and destructors <a id="except.ctor">[[except.ctor]]</a>
|
| 250 |
|
| 251 |
-
As control passes from
|
| 252 |
-
are invoked for all automatic objects constructed
|
| 253 |
-
was entered. The automatic objects are destroyed in
|
| 254 |
-
the completion of their construction.
|
| 255 |
|
| 256 |
An object of any storage duration whose initialization or destruction is
|
| 257 |
terminated by an exception will have destructors executed for all of its
|
| 258 |
fully constructed subobjects (excluding the variant members of a
|
| 259 |
union-like class), that is, for subobjects for which the principal
|
|
@@ -265,15 +269,15 @@ destructor will be invoked. If the object was allocated in a
|
|
| 265 |
*new-expression*, the matching deallocation function (
|
| 266 |
[[basic.stc.dynamic.deallocation]], [[expr.new]], [[class.free]]), if
|
| 267 |
any, is called to free the storage occupied by the object.
|
| 268 |
|
| 269 |
The process of calling destructors for automatic objects constructed on
|
| 270 |
-
the path from a try block to
|
| 271 |
-
unwinding*.” If a destructor called during stack
|
| 272 |
-
exception, `std::terminate` is called (
|
| 273 |
-
destructors should generally catch exceptions
|
| 274 |
-
out of the destructor.
|
| 275 |
|
| 276 |
## Handling an exception <a id="except.handle">[[except.handle]]</a>
|
| 277 |
|
| 278 |
The *exception-declaration* in a *handler* describes the type(s) of
|
| 279 |
exceptions that can cause that *handler* to be entered. The
|
|
@@ -291,22 +295,21 @@ A *handler* is a match for an exception object of type `E` if
|
|
| 291 |
|
| 292 |
- The *handler* is of type *cv* `T` or *cv* `T&` and `E` and `T` are the
|
| 293 |
same type (ignoring the top-level *cv-qualifiers*), or
|
| 294 |
- the *handler* is of type *cv* `T` or *cv* `T&` and `T` is an
|
| 295 |
unambiguous public base class of `E`, or
|
| 296 |
-
- the *handler* is of type *
|
| 297 |
-
|
| 298 |
-
of
|
| 299 |
- a standard pointer conversion ([[conv.ptr]]) not involving
|
| 300 |
conversions to pointers to private or protected or ambiguous classes
|
| 301 |
-
- a qualification conversion
|
| 302 |
-
- the *handler* is
|
| 303 |
-
`std::nullptr_t`.
|
| 304 |
|
| 305 |
-
A *throw-expression* whose operand is an
|
| 306 |
-
|
| 307 |
-
or pointer to member type.
|
| 308 |
|
| 309 |
``` cpp
|
| 310 |
class Matherr { /* ... */ virtual void vf(); };
|
| 311 |
class Overflow: public Matherr { /* ... */ };
|
| 312 |
class Underflow: public Matherr { /* ... */ };
|
|
@@ -341,16 +344,15 @@ try block.
|
|
| 341 |
If no match is found among the handlers for a try block, the search for
|
| 342 |
a matching handler continues in a dynamically surrounding try block of
|
| 343 |
the same thread.
|
| 344 |
|
| 345 |
A handler is considered active when initialization is complete for the
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
throw.
|
| 352 |
|
| 353 |
The exception with the most recently activated handler that is still
|
| 354 |
active is called the *currently handled exception*.
|
| 355 |
|
| 356 |
If no matching handler is found, the function `std::terminate()` is
|
|
@@ -391,25 +393,27 @@ Otherwise, a function returns when control reaches the end of a handler
|
|
| 391 |
for the *function-try-block* ([[stmt.return]]). Flowing off the end of
|
| 392 |
a *function-try-block* is equivalent to a `return` with no value; this
|
| 393 |
results in undefined behavior in a value-returning function (
|
| 394 |
[[stmt.return]]).
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
a name, a temporary ([[class.temporary]]) is copy-initialized (
|
| 400 |
-
[[dcl.init]]) from the exception object. The lifetime of the variable or
|
| 401 |
-
temporary ends when the handler exits, after the destruction of any
|
| 402 |
-
automatic objects initialized within the handler.
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
|
| 412 |
## Exception specifications <a id="except.spec">[[except.spec]]</a>
|
| 413 |
|
| 414 |
A function declaration lists exceptions that its function might directly
|
| 415 |
or indirectly throw by using an *exception-specification* as a suffix of
|
|
@@ -439,11 +443,13 @@ noexcept-specification:
|
|
| 439 |
```
|
| 440 |
|
| 441 |
In a *noexcept-specification*, the *constant-expression*, if supplied,
|
| 442 |
shall be a constant expression ([[expr.const]]) that is contextually
|
| 443 |
converted to `bool` (Clause [[conv]]). A *noexcept-specification*
|
| 444 |
-
`noexcept` is equivalent to `noexcept(
|
|
|
|
|
|
|
| 445 |
|
| 446 |
An *exception-specification* shall appear only on a function declarator
|
| 447 |
for a function type, pointer to function type, reference to function
|
| 448 |
type, or pointer to member function type that is the top-level type of a
|
| 449 |
declaration or definition, or on such a type appearing as a parameter or
|
|
@@ -456,16 +462,16 @@ void (*fp)() throw (int); // OK
|
|
| 456 |
void g(void pfa() throw(int)); // OK
|
| 457 |
typedef int (*pf)() throw(int); // ill-formed
|
| 458 |
```
|
| 459 |
|
| 460 |
A type denoted in an *exception-specification* shall not denote an
|
| 461 |
-
incomplete type. A type denoted in an
|
| 462 |
-
not denote a pointer or reference to an
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
|
| 468 |
Two *exception-specification*s are *compatible* if:
|
| 469 |
|
| 470 |
- both are non-throwing (see below), regardless of their form,
|
| 471 |
- both have the form `noexcept(`*constant-expression*`)` and the
|
|
@@ -599,37 +605,42 @@ yields `true`. A function with a non-throwing *exception-specification*
|
|
| 599 |
does not allow any exceptions.
|
| 600 |
|
| 601 |
An *exception-specification* is not considered part of a function’s
|
| 602 |
type.
|
| 603 |
|
| 604 |
-
An
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
|
|
|
| 611 |
exceptions if any function it directly invokes allows all exceptions,
|
| 612 |
-
and `f`
|
| 613 |
-
allows no exceptions.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 614 |
|
| 615 |
``` cpp
|
| 616 |
struct A {
|
| 617 |
A();
|
| 618 |
A(const A&) throw();
|
| 619 |
A(A&&) throw();
|
| 620 |
~A() throw(X);
|
| 621 |
};
|
| 622 |
struct B {
|
| 623 |
B() throw();
|
| 624 |
-
B(const B&)
|
| 625 |
B(B&&) throw(Y);
|
| 626 |
~B() throw(Y);
|
| 627 |
};
|
| 628 |
struct D : public A, public B {
|
| 629 |
// Implicit declaration of D::D();
|
| 630 |
-
// Implicit declaration of D::D(const D&)
|
| 631 |
// Implicit declaration of D::D(D&&) throw(Y);
|
| 632 |
// Implicit declaration of D::~D() throw(X, Y);
|
| 633 |
};
|
| 634 |
```
|
| 635 |
|
|
@@ -641,10 +652,34 @@ restrictive as that in the base class.
|
|
| 641 |
|
| 642 |
A deallocation function ([[basic.stc.dynamic.deallocation]]) with no
|
| 643 |
explicit *exception-specification* is treated as if it were specified
|
| 644 |
with `noexcept(true)`.
|
| 645 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
In a *dynamic-exception-specification*, a *type-id* followed by an
|
| 647 |
ellipsis is a pack expansion ([[temp.variadic]]).
|
| 648 |
|
| 649 |
The use of *dynamic-exception-specification*s is deprecated (see Annex
|
| 650 |
[[depr]]).
|
|
@@ -754,19 +789,21 @@ exception is rethrown ([[except.throw]]), `std::uncaught_exception()`
|
|
| 754 |
returns `true` from the point of rethrow until the rethrown exception is
|
| 755 |
caught again.
|
| 756 |
|
| 757 |
<!-- Link reference definitions -->
|
| 758 |
[bad.exception]: language.md#bad.exception
|
|
|
|
|
|
|
| 759 |
[basic.start.init]: basic.md#basic.start.init
|
| 760 |
[basic.start.term]: basic.md#basic.start.term
|
| 761 |
[basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
|
| 762 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 763 |
[class.base.init]: special.md#class.base.init
|
| 764 |
[class.copy]: special.md#class.copy
|
| 765 |
[class.dtor]: special.md#class.dtor
|
| 766 |
[class.free]: special.md#class.free
|
| 767 |
-
[class.
|
| 768 |
[conv]: conv.md#conv
|
| 769 |
[conv.ptr]: conv.md#conv.ptr
|
| 770 |
[dcl.init]: dcl.md#dcl.init
|
| 771 |
[depr]: future.md#depr
|
| 772 |
[except]: #except
|
|
@@ -779,19 +816,23 @@ caught again.
|
|
| 779 |
[except.throw]: #except.throw
|
| 780 |
[except.uncaught]: #except.uncaught
|
| 781 |
[except.unexpected]: #except.unexpected
|
| 782 |
[exception.terminate]: language.md#exception.terminate
|
| 783 |
[exception.unexpected]: future.md#exception.unexpected
|
| 784 |
-
[expr.call]: expr.md#expr.call
|
| 785 |
[expr.const]: expr.md#expr.const
|
|
|
|
| 786 |
[expr.new]: expr.md#expr.new
|
|
|
|
| 787 |
[futures]: thread.md#futures
|
|
|
|
|
|
|
| 788 |
[propagation]: language.md#propagation
|
| 789 |
[set.unexpected]: future.md#set.unexpected
|
| 790 |
[special]: special.md#special
|
| 791 |
[stmt.return]: stmt.md#stmt.return
|
| 792 |
[stmt.stmt]: stmt.md#stmt.stmt
|
|
|
|
| 793 |
[support.start.term]: language.md#support.start.term
|
| 794 |
[temp.variadic]: temp.md#temp.variadic
|
| 795 |
[thread.thread.assign]: thread.md#thread.thread.assign
|
| 796 |
[thread.thread.constr]: thread.md#thread.thread.constr
|
| 797 |
[thread.thread.destr]: thread.md#thread.thread.destr
|
|
|
|
| 1 |
# Exception handling <a id="except">[[except]]</a>
|
| 2 |
|
| 3 |
Exception handling provides a way of transferring control and
|
| 4 |
information from a point in the execution of a thread to an exception
|
| 5 |
handler associated with a point previously passed by the execution. A
|
| 6 |
+
handler will be invoked only by throwing an exception in code executed
|
| 7 |
+
in the handler’s try block or in functions called from the handler’s try
|
| 8 |
+
block.
|
| 9 |
|
| 10 |
``` bnf
|
| 11 |
try-block:
|
| 12 |
'try' compound-statement handler-seq
|
| 13 |
```
|
|
|
|
| 38 |
throw-expression:
|
| 39 |
'throw' assignment-expressionₒₚₜ
|
| 40 |
```
|
| 41 |
|
| 42 |
The optional *attribute-specifier-seq* in an *exception-declaration*
|
| 43 |
+
appertains to the parameter of the catch clause ([[except.handle]]).
|
|
|
|
| 44 |
|
| 45 |
A *try-block* is a *statement* (Clause [[stmt.stmt]]). A
|
| 46 |
+
*throw-expression* is of type `void`. Within this Clause “try block” is
|
| 47 |
+
taken to mean both *try-block* and *function-try-block*.
|
|
|
|
|
|
|
| 48 |
|
| 49 |
A `goto` or `switch` statement shall not be used to transfer control
|
| 50 |
into a try block or into a handler.
|
| 51 |
|
| 52 |
``` cpp
|
|
|
|
| 114 |
}
|
| 115 |
```
|
| 116 |
|
| 117 |
## Throwing an exception <a id="except.throw">[[except.throw]]</a>
|
| 118 |
|
| 119 |
+
Throwing an exception transfers control to a handler. An exception can
|
| 120 |
+
be thrown from one of the following contexts: *throw-expression* (see
|
| 121 |
+
below), allocation functions ([[basic.stc.dynamic.allocation]]),
|
| 122 |
+
`dynamic_cast` ([[expr.dynamic.cast]]), `typeid` ([[expr.typeid]]),
|
| 123 |
+
*new-expression* ([[expr.new]]), and standard library functions (
|
| 124 |
+
[[structure.specifications]]). An object is passed and the type of that
|
| 125 |
+
object determines which handlers can catch it.
|
| 126 |
|
| 127 |
``` cpp
|
| 128 |
throw "Help!";
|
| 129 |
```
|
| 130 |
|
|
|
|
| 166 |
handler with a matching type ([[except.handle]]); “nearest” means the
|
| 167 |
handler for which the *compound-statement* or *ctor-initializer*
|
| 168 |
following the `try` keyword was most recently entered by the thread of
|
| 169 |
control and not yet exited.
|
| 170 |
|
| 171 |
+
Throwing an exception copy-initializes ([[dcl.init]], [[class.copy]]) a
|
| 172 |
+
temporary object, called the *exception object*. The temporary is an
|
| 173 |
+
lvalue and is used to initialize the variable declared in the matching
|
| 174 |
+
*handler* ([[except.handle]]). If the type of the exception object
|
| 175 |
+
would be an incomplete type or a pointer to an incomplete type other
|
| 176 |
+
than (possibly cv-qualified) `void` the program is ill-formed.
|
| 177 |
+
Evaluating a *throw-expression* with an operand throws an exception; the
|
| 178 |
+
type of the exception object is determined by removing any top-level
|
| 179 |
+
*cv-qualifiers* from the static type of the operand and adjusting the
|
| 180 |
+
type from “array of `T`” or “function returning `T`” to “pointer to `T`”
|
| 181 |
+
or “pointer to function returning `T`,” respectively.
|
|
|
|
|
|
|
| 182 |
|
| 183 |
The memory for the exception object is allocated in an unspecified way,
|
| 184 |
except as noted in [[basic.stc.dynamic.allocation]]. If a handler exits
|
| 185 |
by rethrowing, control is passed to another handler for the same
|
| 186 |
exception. The exception object is destroyed after either the last
|
|
|
|
| 191 |
handler exits, immediately after the destruction of the object declared
|
| 192 |
in the *exception-declaration* in the handler, if any. In the latter
|
| 193 |
case, the destruction occurs before the destructor of
|
| 194 |
`std::exception_ptr` returns. The implementation may then deallocate the
|
| 195 |
memory for the exception object; any such deallocation is done in an
|
| 196 |
+
unspecified way. a thrown exception does not propagate to other threads
|
| 197 |
+
unless caught, stored, and rethrown using appropriate library functions;
|
| 198 |
+
see [[propagation]] and [[futures]].
|
| 199 |
|
| 200 |
+
When the thrown object is a class object, the constructor selected for
|
| 201 |
+
the copy-initialization and the destructor shall be accessible, even if
|
| 202 |
+
the copy/move operation is elided ([[class.copy]]).
|
| 203 |
|
| 204 |
An exception is considered caught when a handler for that exception
|
| 205 |
becomes active ([[except.handle]]). An exception can have active
|
| 206 |
handlers and still be considered uncaught if it is rethrown.
|
| 207 |
|
| 208 |
+
If the exception handling mechanism, after completing the initialization
|
| 209 |
+
of the exception object but before the activation of a handler for the
|
| 210 |
+
exception, calls a function that exits via an exception,
|
| 211 |
+
`std::terminate` is called ([[except.terminate]]).
|
| 212 |
|
| 213 |
``` cpp
|
| 214 |
struct C {
|
| 215 |
C() { }
|
| 216 |
+
C(const C&) {
|
| 217 |
+
if (std::uncaught_exception()) {
|
| 218 |
+
throw 0; // throw during copy to handler's exception-declaration object~([except.handle])
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
};
|
| 222 |
|
| 223 |
int main() {
|
| 224 |
try {
|
| 225 |
+
throw C(); // calls std::terminate() if construction of the handler's
|
| 226 |
+
// exception-declaration object is not elided~([class.copy])
|
| 227 |
} catch(C) { }
|
| 228 |
}
|
| 229 |
```
|
| 230 |
|
| 231 |
A *throw-expression* with no operand rethrows the currently handled
|
| 232 |
exception ([[except.handle]]). The exception is reactivated with the
|
| 233 |
+
existing exception object; no new exception object is created. The
|
| 234 |
exception is no longer considered to be caught; therefore, the value of
|
| 235 |
`std::uncaught_exception()` will again be `true`. code that must be
|
| 236 |
executed because of an exception yet cannot completely handle the
|
| 237 |
exception can be written like this:
|
| 238 |
|
|
|
|
| 250 |
*throw-expression* with no operand calls `std::terminate()` (
|
| 251 |
[[except.terminate]]).
|
| 252 |
|
| 253 |
## Constructors and destructors <a id="except.ctor">[[except.ctor]]</a>
|
| 254 |
|
| 255 |
+
As control passes from the point where an exception is thrown to a
|
| 256 |
+
handler, destructors are invoked for all automatic objects constructed
|
| 257 |
+
since the try block was entered. The automatic objects are destroyed in
|
| 258 |
+
the reverse order of the completion of their construction.
|
| 259 |
|
| 260 |
An object of any storage duration whose initialization or destruction is
|
| 261 |
terminated by an exception will have destructors executed for all of its
|
| 262 |
fully constructed subobjects (excluding the variant members of a
|
| 263 |
union-like class), that is, for subobjects for which the principal
|
|
|
|
| 269 |
*new-expression*, the matching deallocation function (
|
| 270 |
[[basic.stc.dynamic.deallocation]], [[expr.new]], [[class.free]]), if
|
| 271 |
any, is called to free the storage occupied by the object.
|
| 272 |
|
| 273 |
The process of calling destructors for automatic objects constructed on
|
| 274 |
+
the path from a try block to the point where an exception is thrown is
|
| 275 |
+
called “*stack unwinding*.” If a destructor called during stack
|
| 276 |
+
unwinding exits with an exception, `std::terminate` is called (
|
| 277 |
+
[[except.terminate]]). So destructors should generally catch exceptions
|
| 278 |
+
and not let them propagate out of the destructor.
|
| 279 |
|
| 280 |
## Handling an exception <a id="except.handle">[[except.handle]]</a>
|
| 281 |
|
| 282 |
The *exception-declaration* in a *handler* describes the type(s) of
|
| 283 |
exceptions that can cause that *handler* to be entered. The
|
|
|
|
| 295 |
|
| 296 |
- The *handler* is of type *cv* `T` or *cv* `T&` and `E` and `T` are the
|
| 297 |
same type (ignoring the top-level *cv-qualifiers*), or
|
| 298 |
- the *handler* is of type *cv* `T` or *cv* `T&` and `T` is an
|
| 299 |
unambiguous public base class of `E`, or
|
| 300 |
+
- the *handler* is of type *cv* `T` or `const T&` where `T` is a pointer
|
| 301 |
+
type and `E` is a pointer type that can be converted to `T` by either
|
| 302 |
+
or both of
|
| 303 |
- a standard pointer conversion ([[conv.ptr]]) not involving
|
| 304 |
conversions to pointers to private or protected or ambiguous classes
|
| 305 |
+
- a qualification conversion, or
|
| 306 |
+
- the *handler* is of type *cv* `T` or `const T&` where `T` is a pointer
|
| 307 |
+
or pointer to member type and `E` is `std::nullptr_t`.
|
| 308 |
|
| 309 |
+
A *throw-expression* whose operand is an integer literal with value zero
|
| 310 |
+
does not match a handler of pointer or pointer to member type.
|
|
|
|
| 311 |
|
| 312 |
``` cpp
|
| 313 |
class Matherr { /* ... */ virtual void vf(); };
|
| 314 |
class Overflow: public Matherr { /* ... */ };
|
| 315 |
class Underflow: public Matherr { /* ... */ };
|
|
|
|
| 344 |
If no match is found among the handlers for a try block, the search for
|
| 345 |
a matching handler continues in a dynamically surrounding try block of
|
| 346 |
the same thread.
|
| 347 |
|
| 348 |
A handler is considered active when initialization is complete for the
|
| 349 |
+
parameter (if any) of the catch clause. The stack will have been unwound
|
| 350 |
+
at that point. Also, an implicit handler is considered active when
|
| 351 |
+
`std::terminate()` or `std::unexpected()` is entered due to a throw. A
|
| 352 |
+
handler is no longer considered active when the catch clause exits or
|
| 353 |
+
when `std::unexpected()` exits after being entered due to a throw.
|
|
|
|
| 354 |
|
| 355 |
The exception with the most recently activated handler that is still
|
| 356 |
active is called the *currently handled exception*.
|
| 357 |
|
| 358 |
If no matching handler is found, the function `std::terminate()` is
|
|
|
|
| 393 |
for the *function-try-block* ([[stmt.return]]). Flowing off the end of
|
| 394 |
a *function-try-block* is equivalent to a `return` with no value; this
|
| 395 |
results in undefined behavior in a value-returning function (
|
| 396 |
[[stmt.return]]).
|
| 397 |
|
| 398 |
+
The variable declared by the *exception-declaration*, of type cv `T` or
|
| 399 |
+
cv `T&`, is initialized from the exception object, of type `E`, as
|
| 400 |
+
follows:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
- if `T` is a base class of `E`, the variable is copy-initialized (
|
| 403 |
+
[[dcl.init]]) from the corresponding base class subobject of the
|
| 404 |
+
exception object;
|
| 405 |
+
- otherwise, the variable is copy-initialized ([[dcl.init]]) from the
|
| 406 |
+
exception object.
|
| 407 |
+
|
| 408 |
+
The lifetime of the variable ends when the handler exits, after the
|
| 409 |
+
destruction of any automatic objects initialized within the handler.
|
| 410 |
+
|
| 411 |
+
When the handler declares an object, any changes to that object will not
|
| 412 |
+
affect the exception object. When the handler declares a reference to an
|
| 413 |
+
object, any changes to the referenced object are changes to the
|
| 414 |
+
exception object and will have effect should that object be rethrown.
|
| 415 |
|
| 416 |
## Exception specifications <a id="except.spec">[[except.spec]]</a>
|
| 417 |
|
| 418 |
A function declaration lists exceptions that its function might directly
|
| 419 |
or indirectly throw by using an *exception-specification* as a suffix of
|
|
|
|
| 443 |
```
|
| 444 |
|
| 445 |
In a *noexcept-specification*, the *constant-expression*, if supplied,
|
| 446 |
shall be a constant expression ([[expr.const]]) that is contextually
|
| 447 |
converted to `bool` (Clause [[conv]]). A *noexcept-specification*
|
| 448 |
+
`noexcept` is equivalent to `noexcept(true)`. A `(` token that follows
|
| 449 |
+
`noexcept` is part of the *noexcept-specification* and does not commence
|
| 450 |
+
an initializer ([[dcl.init]]).
|
| 451 |
|
| 452 |
An *exception-specification* shall appear only on a function declarator
|
| 453 |
for a function type, pointer to function type, reference to function
|
| 454 |
type, or pointer to member function type that is the top-level type of a
|
| 455 |
declaration or definition, or on such a type appearing as a parameter or
|
|
|
|
| 462 |
void g(void pfa() throw(int)); // OK
|
| 463 |
typedef int (*pf)() throw(int); // ill-formed
|
| 464 |
```
|
| 465 |
|
| 466 |
A type denoted in an *exception-specification* shall not denote an
|
| 467 |
+
incomplete type or an rvalue reference type. A type denoted in an
|
| 468 |
+
*exception-specification* shall not denote a pointer or reference to an
|
| 469 |
+
incomplete type, other than *cv* `void*`. A type cv `T`, “array of `T`”,
|
| 470 |
+
or “function returning `T`” denoted in an *exception-specification* is
|
| 471 |
+
adjusted to type `T`, “pointer to `T`”, or “pointer to function
|
| 472 |
+
returning `T`”, respectively.
|
| 473 |
|
| 474 |
Two *exception-specification*s are *compatible* if:
|
| 475 |
|
| 476 |
- both are non-throwing (see below), regardless of their form,
|
| 477 |
- both have the form `noexcept(`*constant-expression*`)` and the
|
|
|
|
| 605 |
does not allow any exceptions.
|
| 606 |
|
| 607 |
An *exception-specification* is not considered part of a function’s
|
| 608 |
type.
|
| 609 |
|
| 610 |
+
An inheriting constructor ([[class.inhctor]]) and an implicitly
|
| 611 |
+
declared special member function (Clause [[special]]) have an
|
| 612 |
+
*exception-specification*. If `f` is an inheriting constructor or an
|
| 613 |
+
implicitly declared default constructor, copy constructor, move
|
| 614 |
+
constructor, destructor, copy assignment operator, or move assignment
|
| 615 |
+
operator, its implicit *exception-specification* specifies the *type-id*
|
| 616 |
+
`T` if and only if `T` is allowed by the *exception-specification* of a
|
| 617 |
+
function directly invoked by `f`’s implicit definition; `f` allows all
|
| 618 |
exceptions if any function it directly invokes allows all exceptions,
|
| 619 |
+
and `f` has the *exception-specification* `noexcept(true)` if every
|
| 620 |
+
function it directly invokes allows no exceptions. It follows that `f`
|
| 621 |
+
has the *exception-specification* `noexcept(true)` if it invokes no
|
| 622 |
+
other functions. An instantiation of an inheriting constructor template
|
| 623 |
+
has an implied *exception-specification* as if it were a non-template
|
| 624 |
+
inheriting constructor.
|
| 625 |
|
| 626 |
``` cpp
|
| 627 |
struct A {
|
| 628 |
A();
|
| 629 |
A(const A&) throw();
|
| 630 |
A(A&&) throw();
|
| 631 |
~A() throw(X);
|
| 632 |
};
|
| 633 |
struct B {
|
| 634 |
B() throw();
|
| 635 |
+
B(const B&) = default; // Declaration of B::B(const B&) noexcept(true)
|
| 636 |
B(B&&) throw(Y);
|
| 637 |
~B() throw(Y);
|
| 638 |
};
|
| 639 |
struct D : public A, public B {
|
| 640 |
// Implicit declaration of D::D();
|
| 641 |
+
// Implicit declaration of D::D(const D&) noexcept(true);
|
| 642 |
// Implicit declaration of D::D(D&&) throw(Y);
|
| 643 |
// Implicit declaration of D::~D() throw(X, Y);
|
| 644 |
};
|
| 645 |
```
|
| 646 |
|
|
|
|
| 652 |
|
| 653 |
A deallocation function ([[basic.stc.dynamic.deallocation]]) with no
|
| 654 |
explicit *exception-specification* is treated as if it were specified
|
| 655 |
with `noexcept(true)`.
|
| 656 |
|
| 657 |
+
An *exception-specification* is considered to be *needed* when:
|
| 658 |
+
|
| 659 |
+
- in an expression, the function is the unique lookup result or the
|
| 660 |
+
selected member of a set of overloaded functions ([[basic.lookup]],
|
| 661 |
+
[[over.match]], [[over.over]]);
|
| 662 |
+
- the function is odr-used ([[basic.def.odr]]) or, if it appears in an
|
| 663 |
+
unevaluated operand, would be odr-used if the expression were
|
| 664 |
+
potentially-evaluated;
|
| 665 |
+
- the *exception-specification* is compared to that of another
|
| 666 |
+
declaration (e.g., an explicit specialization or an overriding virtual
|
| 667 |
+
function);
|
| 668 |
+
- the function is defined; or
|
| 669 |
+
- the *exception-specification* is needed for a defaulted special member
|
| 670 |
+
function that calls the function. A defaulted declaration does not
|
| 671 |
+
require the *exception-specification* of a base member function to be
|
| 672 |
+
evaluated until the implicit *exception-specification* of the derived
|
| 673 |
+
function is needed, but an explicit *exception-specification* needs
|
| 674 |
+
the implicit *exception-specification* to compare against.
|
| 675 |
+
|
| 676 |
+
The *exception-specification* of a defaulted special member function is
|
| 677 |
+
evaluated as described above only when needed; similarly, the
|
| 678 |
+
*exception-specification* of a specialization of a function template or
|
| 679 |
+
member function of a class template is instantiated only when needed.
|
| 680 |
+
|
| 681 |
In a *dynamic-exception-specification*, a *type-id* followed by an
|
| 682 |
ellipsis is a pack expansion ([[temp.variadic]]).
|
| 683 |
|
| 684 |
The use of *dynamic-exception-specification*s is deprecated (see Annex
|
| 685 |
[[depr]]).
|
|
|
|
| 789 |
returns `true` from the point of rethrow until the rethrown exception is
|
| 790 |
caught again.
|
| 791 |
|
| 792 |
<!-- Link reference definitions -->
|
| 793 |
[bad.exception]: language.md#bad.exception
|
| 794 |
+
[basic.def.odr]: basic.md#basic.def.odr
|
| 795 |
+
[basic.lookup]: basic.md#basic.lookup
|
| 796 |
[basic.start.init]: basic.md#basic.start.init
|
| 797 |
[basic.start.term]: basic.md#basic.start.term
|
| 798 |
[basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
|
| 799 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 800 |
[class.base.init]: special.md#class.base.init
|
| 801 |
[class.copy]: special.md#class.copy
|
| 802 |
[class.dtor]: special.md#class.dtor
|
| 803 |
[class.free]: special.md#class.free
|
| 804 |
+
[class.inhctor]: special.md#class.inhctor
|
| 805 |
[conv]: conv.md#conv
|
| 806 |
[conv.ptr]: conv.md#conv.ptr
|
| 807 |
[dcl.init]: dcl.md#dcl.init
|
| 808 |
[depr]: future.md#depr
|
| 809 |
[except]: #except
|
|
|
|
| 816 |
[except.throw]: #except.throw
|
| 817 |
[except.uncaught]: #except.uncaught
|
| 818 |
[except.unexpected]: #except.unexpected
|
| 819 |
[exception.terminate]: language.md#exception.terminate
|
| 820 |
[exception.unexpected]: future.md#exception.unexpected
|
|
|
|
| 821 |
[expr.const]: expr.md#expr.const
|
| 822 |
+
[expr.dynamic.cast]: expr.md#expr.dynamic.cast
|
| 823 |
[expr.new]: expr.md#expr.new
|
| 824 |
+
[expr.typeid]: expr.md#expr.typeid
|
| 825 |
[futures]: thread.md#futures
|
| 826 |
+
[over.match]: over.md#over.match
|
| 827 |
+
[over.over]: over.md#over.over
|
| 828 |
[propagation]: language.md#propagation
|
| 829 |
[set.unexpected]: future.md#set.unexpected
|
| 830 |
[special]: special.md#special
|
| 831 |
[stmt.return]: stmt.md#stmt.return
|
| 832 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 833 |
+
[structure.specifications]: library.md#structure.specifications
|
| 834 |
[support.start.term]: language.md#support.start.term
|
| 835 |
[temp.variadic]: temp.md#temp.variadic
|
| 836 |
[thread.thread.assign]: thread.md#thread.thread.assign
|
| 837 |
[thread.thread.constr]: thread.md#thread.thread.constr
|
| 838 |
[thread.thread.destr]: thread.md#thread.thread.destr
|