From Jason Turner

[except]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmps1khthk7/{from.md → to.md} +121 -88
tmp/tmps1khthk7/{from.md → to.md} RENAMED
@@ -9,26 +9,26 @@ handler will be invoked only by throwing an exception in code executed
9
  in the handler’s try block or in functions called from the handler’s try
10
  block.
11
 
12
  ``` bnf
13
  try-block:
14
- 'try' compound-statement handler-seq
15
  ```
16
 
17
  ``` bnf
18
  function-try-block:
19
- 'try' ctor-initializerₒₚₜ compound-statement handler-seq
20
  ```
21
 
22
  ``` bnf
23
  handler-seq:
24
  handler handler-seqₒₚₜ
25
  ```
26
 
27
  ``` bnf
28
  handler:
29
- 'catch' '(' exception-declaration ')' compound-statement
30
  ```
31
 
32
  ``` bnf
33
  exception-declaration:
34
  attribute-specifier-seqₒₚₜ type-specifier-seq declarator
@@ -42,12 +42,12 @@ appertains to the parameter of the catch clause [[except.handle]].
42
  A *try-block* is a *statement* [[stmt.pre]].
43
 
44
  [*Note 1*: Within this Clause “try block” is taken to mean both
45
  *try-block* and *function-try-block*. — *end note*]
46
 
47
- A `goto` or `switch` statement shall not be used to transfer control
48
- into a try block or into a handler.
49
 
50
  [*Example 1*:
51
 
52
  ``` cpp
53
  void f() {
@@ -183,14 +183,12 @@ When an exception is thrown, control is transferred to the nearest
183
  handler with a matching type [[except.handle]]; “nearest” means the
184
  handler for which the *compound-statement* or *ctor-initializer*
185
  following the `try` keyword was most recently entered by the thread of
186
  control and not yet exited.
187
 
188
- Throwing an exception copy-initializes ([[dcl.init]],
189
- [[class.copy.ctor]]) a temporary object, called the *exception object*.
190
- An lvalue denoting the temporary is used to initialize the variable
191
- declared in the matching *handler* [[except.handle]]. If the type of the
192
  exception object would be an incomplete type, an abstract class type
193
  [[class.abstract]], or a pointer to an incomplete type other than
194
  cv `void` the program is ill-formed.
195
 
196
  The memory for the exception object is allocated in an unspecified way,
@@ -233,11 +231,12 @@ becomes active [[except.handle]].
233
  [*Note 4*: An exception can have active handlers and still be
234
  considered uncaught if it is rethrown. — *end note*]
235
 
236
  If the exception handling mechanism handling an uncaught exception
237
  [[except.uncaught]] directly invokes a function that exits via an
238
- exception, the function `std::terminate` is called [[except.terminate]].
 
239
 
240
  [*Example 2*:
241
 
242
  ``` cpp
243
  struct C {
@@ -257,22 +256,18 @@ int main() {
257
  }
258
  ```
259
 
260
  — *end example*]
261
 
262
- [*Note 5*:
263
-
264
- Consequently, destructors should generally catch exceptions and not let
265
- them propagate.
266
-
267
- — *end note*]
268
 
269
  ## Constructors and destructors <a id="except.ctor">[[except.ctor]]</a>
270
 
271
  As control passes from the point where an exception is thrown to a
272
- handler, objects with automatic storage duration are destroyed by a
273
- process, specified in this subclause, called *stack unwinding*.
274
 
275
  Each object with automatic storage duration is destroyed if it has been
276
  constructed, but not yet destroyed, since the try block was entered. If
277
  an exception is thrown during the destruction of temporaries or local
278
  variables for a `return` statement [[stmt.return]], the destructor for
@@ -304,23 +299,47 @@ destroyed, causing stack unwinding, resulting in the destruction of the
304
  returned object, followed by the destruction of the local variable `a`.
305
  Finally, the returned object is constructed again at \#2.
306
 
307
  — *end example*]
308
 
309
- If the initialization or destruction of an object other than by
310
- delegating constructor is terminated by an exception, the destructor is
311
- invoked for each of the object’s direct subobjects and, for a complete
312
- object, virtual base class subobjects, whose initialization has
313
- completed [[dcl.init]] and whose destructor has not yet begun execution,
314
- except that in the case of destruction, the variant members of a
315
- union-like class are not destroyed.
316
 
317
  [*Note 1*: If such an object has a reference member that extends the
318
  lifetime of a temporary object, this ends the lifetime of the reference
319
  member, so the lifetime of the temporary object is effectively not
320
  extended. — *end note*]
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  The subobjects are destroyed in the reverse order of the completion of
323
  their construction. Such destruction is sequenced before entering a
324
  handler of the *function-try-block* of the constructor or destructor, if
325
  any.
326
 
@@ -328,11 +347,11 @@ If the *compound-statement* of the *function-body* of a delegating
328
  constructor for an object exits via an exception, the object’s
329
  destructor is invoked. Such destruction is sequenced before entering a
330
  handler of the *function-try-block* of a delegating constructor for that
331
  object, if any.
332
 
333
- [*Note 2*: If the object was allocated by a *new-expression*
334
  [[expr.new]], the matching deallocation function
335
  [[basic.stc.dynamic.deallocation]], if any, is called to free the
336
  storage occupied by the object. — *end note*]
337
 
338
  ## Handling an exception <a id="except.handle">[[except.handle]]</a>
@@ -340,12 +359,11 @@ storage occupied by the object. — *end note*]
340
  The *exception-declaration* in a *handler* describes the type(s) of
341
  exceptions that can cause that *handler* to be entered. The
342
  *exception-declaration* shall not denote an incomplete type, an abstract
343
  class type, or an rvalue reference type. The *exception-declaration*
344
  shall not denote a pointer or reference to an incomplete type, other
345
- than `void*`, `const` `void*`, `volatile` `void*`, or `const` `volatile`
346
- `void*`.
347
 
348
  A handler of type “array of `T`” or function type `T` is adjusted to be
349
  of type “pointer to `T`”.
350
 
351
  A *handler* is a match for an exception object of type `E` if
@@ -423,27 +441,24 @@ considered active when the catch clause exits.
423
 
424
  The exception with the most recently activated handler that is still
425
  active is called the *currently handled exception*.
426
 
427
  If no matching handler is found, the function `std::terminate` is
428
- called; whether or not the stack is unwound before this call to
429
  `std::terminate` is *implementation-defined* [[except.terminate]].
430
 
431
  Referring to any non-static member or base class of an object in the
432
  handler for a *function-try-block* of a constructor or destructor for
433
  that object results in undefined behavior.
434
 
435
- The scope and lifetime of the parameters of a function or constructor
436
- extend into the handlers of a *function-try-block*.
437
-
438
  Exceptions thrown in destructors of objects with static storage duration
439
- or in constructors of namespace-scope objects with static storage
440
- duration are not caught by a *function-try-block* on the `main` function
441
- [[basic.start.main]]. Exceptions thrown in destructors of objects with
442
- thread storage duration or in constructors of namespace-scope objects
443
- with thread storage duration are not caught by a *function-try-block* on
444
- the initial function of the thread.
445
 
446
  If a `return` statement [[stmt.return]] appears in a handler of the
447
  *function-try-block* of a constructor, the program is ill-formed.
448
 
449
  The currently handled exception is rethrown if control reaches the end
@@ -455,14 +470,14 @@ of the *compound-statement* of that function (see [[stmt.return]]).
455
  The variable declared by the *exception-declaration*, of type cv `T` or
456
  cv `T&`, is initialized from the exception object, of type `E`, as
457
  follows:
458
 
459
  - if `T` is a base class of `E`, the variable is copy-initialized
460
- [[dcl.init]] from the corresponding base class subobject of the
461
- exception object;
462
- - otherwise, the variable is copy-initialized [[dcl.init]] from the
463
- exception object.
464
 
465
  The lifetime of the variable ends when the handler exits, after the
466
  destruction of any objects with automatic storage duration initialized
467
  within the handler.
468
 
@@ -481,12 +496,12 @@ specification*. The exception specification is either defined
481
  implicitly, or defined explicitly by using a *noexcept-specifier* as a
482
  suffix of a function declarator [[dcl.fct]].
483
 
484
  ``` bnf
485
  noexcept-specifier:
486
- 'noexcept' '(' constant-expression ')'
487
- 'noexcept'
488
  ```
489
 
490
  In a *noexcept-specifier*, the *constant-expression*, if supplied, shall
491
  be a contextually converted constant expression of type `bool`
492
  [[expr.const]]; that constant expression is the exception specification
@@ -494,30 +509,39 @@ of the function type in which the *noexcept-specifier* appears. A `(`
494
  token that follows `noexcept` is part of the *noexcept-specifier* and
495
  does not commence an initializer [[dcl.init]]. The *noexcept-specifier*
496
  `noexcept` without a *constant-expression* is equivalent to the
497
  *noexcept-specifier* `noexcept(true)`.
498
 
 
 
 
 
 
 
 
 
 
499
  If a declaration of a function does not have a *noexcept-specifier*, the
500
  declaration has a potentially throwing exception specification unless it
501
  is a destructor or a deallocation function or is defaulted on its first
502
  declaration, in which cases the exception specification is as specified
503
  below and no other declaration for that function shall have a
504
  *noexcept-specifier*. In an explicit instantiation [[temp.explicit]] a
505
  *noexcept-specifier* may be specified, but is not required. If a
506
- *noexcept-specifier* is specified in an explicit instantiation
507
- directive, the exception specification shall be the same as the
508
- exception specification of all other declarations of that function. A
509
- diagnostic is required only if the exception specifications are not the
510
- same within a single translation unit.
511
 
512
  If a virtual function has a non-throwing exception specification, all
513
  declarations, including the definition, of any function that overrides
514
  that virtual function in any derived class shall have a non-throwing
515
  exception specification, unless the overriding function is defined as
516
  deleted.
517
 
518
- [*Example 1*:
519
 
520
  ``` cpp
521
  struct B {
522
  virtual void f() noexcept;
523
  virtual void g();
@@ -538,29 +562,29 @@ non-throwing exception specification.
538
  — *end example*]
539
 
540
  Whenever an exception is thrown and the search for a handler
541
  [[except.handle]] encounters the outermost block of a function with a
542
  non-throwing exception specification, the function `std::terminate` is
543
- called [[except.terminate]].
544
 
545
  [*Note 1*: An implementation is not permitted to reject an expression
546
  merely because, when executed, it throws or might throw an exception
547
  from a function with a non-throwing exception
548
  specification. — *end note*]
549
 
550
- [*Example 2*:
551
 
552
  ``` cpp
553
  extern void f(); // potentially-throwing
554
 
555
  void g() noexcept {
556
  f(); // valid, even if f throws
557
  throw 42; // valid, effectively a call to std::terminate
558
  }
559
  ```
560
 
561
- The call to `f` is well-formed even though, when called, `f` might throw
562
  an exception.
563
 
564
  — *end example*]
565
 
566
  An expression E is *potentially-throwing* if
@@ -569,11 +593,12 @@ An expression E is *potentially-throwing* if
569
  function type, or a pointer-to-function type, with a
570
  potentially-throwing exception specification, or
571
  - E implicitly invokes a function (such as an overloaded operator, an
572
  allocation function in a *new-expression*, a constructor for a
573
  function argument, or a destructor if E is a full-expression
574
- [[intro.execution]]) that is potentially-throwing, or
 
575
  - E is a *throw-expression* [[expr.throw]], or
576
  - E is a `dynamic_cast` expression that casts to a reference type and
577
  requires a runtime check [[expr.dynamic.cast]], or
578
  - E is a `typeid` expression applied to a (possibly parenthesized)
579
  built-in unary `*` operator applied to a pointer to a polymorphic
@@ -584,31 +609,31 @@ An expression E is *potentially-throwing* if
584
  An implicitly-declared constructor for a class `X`, or a constructor
585
  without a *noexcept-specifier* that is defaulted on its first
586
  declaration, has a potentially-throwing exception specification if and
587
  only if any of the following constructs is potentially-throwing:
588
 
589
- - a constructor selected by overload resolution in the implicit
590
- definition of the constructor for class `X` to initialize a
591
  potentially constructed subobject, or
592
  - a subexpression of such an initialization, such as a default argument
593
  expression, or,
594
  - for a default constructor, a default member initializer.
595
 
596
  [*Note 2*: Even though destructors for fully-constructed subobjects are
597
  invoked when an exception is thrown during the execution of a
598
  constructor [[except.ctor]], their exception specifications do not
599
  contribute to the exception specification of the constructor, because an
600
  exception thrown from such a destructor would call the function
601
- `std::terminate` rather than escape the constructor ([[except.throw]],
602
- [[except.terminate]]). — *end note*]
603
 
604
  The exception specification for an implicitly-declared destructor, or a
605
  destructor without a *noexcept-specifier*, is potentially-throwing if
606
  and only if any of the destructors for any of its potentially
607
- constructed subobjects is potentially-throwing or the destructor is
608
- virtual and the destructor of any virtual base class is
609
- potentially-throwing.
610
 
611
  The exception specification for an implicitly-declared assignment
612
  operator, or an assignment-operator without a *noexcept-specifier* that
613
  is defaulted on its first declaration, is potentially-throwing if and
614
  only if the invocation of any assignment operator in the implicit
@@ -621,11 +646,11 @@ specification.
621
  The exception specification for a comparison operator function
622
  [[over.binary]] without a *noexcept-specifier* that is defaulted on its
623
  first declaration is potentially-throwing if and only if any expression
624
  in the implicit definition is potentially-throwing.
625
 
626
- [*Example 3*:
627
 
628
  ``` cpp
629
  struct A {
630
  A(int = (A(5), 0)) noexcept;
631
  A(const A&) noexcept;
@@ -655,14 +680,13 @@ base class function has a non-throwing exception specification.
655
 
656
  — *end example*]
657
 
658
  An exception specification is considered to be *needed* when:
659
 
660
- - in an expression, the function is the unique lookup result or the
661
- selected member of a set of overloaded functions ([[basic.lookup]],
662
- [[over.match]], [[over.over]]);
663
- - the function is odr-used [[basic.def.odr]] or, if it appears in an
664
  unevaluated operand, would be odr-used if the expression were
665
  potentially-evaluated;
666
  - the exception specification is compared to that of another declaration
667
  (e.g., an explicit specialization or an overriding virtual function);
668
  - the function is defined; or
@@ -678,21 +702,23 @@ described above only when needed; similarly, the *noexcept-specifier* of
678
  a specialization of a function template or member function of a class
679
  template is instantiated only when needed.
680
 
681
  ## Special functions <a id="except.special">[[except.special]]</a>
682
 
 
 
683
  The function `std::terminate` [[except.terminate]] is used by the
684
  exception handling mechanism for coping with errors related to the
685
  exception handling mechanism itself. The function
686
  `std::current_exception()` [[propagation]] and the class
687
  `std::nested_exception` [[except.nested]] can be used by a program to
688
  capture the currently handled exception.
689
 
690
  ### The `std::terminate` function <a id="except.terminate">[[except.terminate]]</a>
691
 
692
- In some situations exception handling must be abandoned for less subtle
693
- error handling techniques.
694
 
695
  [*Note 1*:
696
 
697
  These situations are:
698
 
@@ -705,11 +731,11 @@ These situations are:
705
  - when the search for a handler [[except.handle]] encounters the
706
  outermost block of a function with a non-throwing exception
707
  specification [[except.spec]], or
708
  - when the destruction of an object during stack unwinding
709
  [[except.ctor]] terminates by throwing an exception, or
710
- - when initialization of a non-local variable with static or thread
711
  storage duration [[basic.start.dynamic]] exits via an exception, or
712
  - when destruction of an object with static or thread storage duration
713
  exits via an exception [[basic.start.term]], or
714
  - when execution of a function registered with `std::atexit` or
715
  `std::at_quick_exit` exits via an exception [[support.start.term]], or
@@ -719,78 +745,82 @@ These situations are:
719
  - when the function `std::nested_exception::rethrow_nested` is called
720
  for an object that has captured no exception [[except.nested]], or
721
  - when execution of the initial function of a thread exits via an
722
  exception [[thread.thread.constr]], or
723
  - for a parallel algorithm whose `ExecutionPolicy` specifies such
724
- behavior ([[execpol.seq]], [[execpol.par]], [[execpol.parunseq]]),
725
- when execution of an element access function
726
- [[algorithms.parallel.defns]] of the parallel algorithm exits via an
727
- exception [[algorithms.parallel.exceptions]], or
728
  - when the destructor or the move assignment operator is invoked on an
729
- object of type `std::thread` that refers to a joinable thread (
730
- [[thread.thread.destr]], [[thread.thread.assign]]), or
731
  - when a call to a `wait()`, `wait_until()`, or `wait_for()` function on
732
- a condition variable ([[thread.condition.condvar]],
733
- [[thread.condition.condvarany]]) fails to meet a postcondition.
 
734
 
735
  — *end note*]
736
 
737
- In such cases, the function `std::terminate` is called
738
  [[exception.terminate]]. In the situation where no matching handler is
739
  found, it is *implementation-defined* whether or not the stack is
740
- unwound before `std::terminate` is called. In the situation where the
741
  search for a handler [[except.handle]] encounters the outermost block of
742
  a function with a non-throwing exception specification [[except.spec]],
743
  it is *implementation-defined* whether the stack is unwound, unwound
744
  partially, or not unwound at all before the function `std::terminate` is
745
- called. In all other situations, the stack shall not be unwound before
746
- the function `std::terminate` is called. An implementation is not
747
  permitted to finish stack unwinding prematurely based on a determination
748
- that the unwind process will eventually cause a call to the function
749
- `std::terminate`.
750
 
751
- ### The `std::uncaught_exceptions()` function <a id="except.uncaught">[[except.uncaught]]</a>
752
 
753
  An exception is considered uncaught after completing the initialization
754
  of the exception object [[except.throw]] until completing the activation
755
  of a handler for the exception [[except.handle]].
756
 
757
  [*Note 1*: As a consequence, an exception is considered uncaught during
758
  any stack unwinding resulting from it being thrown. — *end note*]
759
 
760
- If an exception is rethrown ([[expr.throw]], [[propagation]]), it is
761
  considered uncaught from the point of rethrow until the rethrown
762
- exception is caught. The function `std::uncaught_exceptions()`
763
  [[uncaught.exceptions]] returns the number of uncaught exceptions in the
764
  current thread.
765
 
766
  <!-- Link reference definitions -->
767
  [algorithms.parallel.defns]: algorithms.md#algorithms.parallel.defns
768
  [algorithms.parallel.exceptions]: algorithms.md#algorithms.parallel.exceptions
769
- [basic.def.odr]: basic.md#basic.def.odr
770
- [basic.lookup]: basic.md#basic.lookup
771
  [basic.start.dynamic]: basic.md#basic.start.dynamic
772
  [basic.start.main]: basic.md#basic.start.main
773
  [basic.start.term]: basic.md#basic.start.term
774
  [basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
775
  [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
776
  [class.abstract]: class.md#class.abstract
 
777
  [class.copy.ctor]: class.md#class.copy.ctor
778
  [class.copy.elision]: class.md#class.copy.elision
779
  [class.dtor]: class.md#class.dtor
 
780
  [conv.fctptr]: expr.md#conv.fctptr
781
  [conv.ptr]: expr.md#conv.ptr
782
  [conv.qual]: expr.md#conv.qual
783
  [dcl.fct]: dcl.md#dcl.fct
784
  [dcl.init]: dcl.md#dcl.init
 
 
785
  [except]: #except
786
  [except.ctor]: #except.ctor
787
  [except.handle]: #except.handle
788
  [except.nested]: support.md#except.nested
789
  [except.pre]: #except.pre
790
  [except.spec]: #except.spec
791
  [except.special]: #except.special
 
792
  [except.terminate]: #except.terminate
793
  [except.throw]: #except.throw
794
  [except.uncaught]: #except.uncaught
795
  [exception.terminate]: support.md#exception.terminate
796
  [execpol.par]: utilities.md#execpol.par
@@ -798,25 +828,28 @@ current thread.
798
  [execpol.seq]: utilities.md#execpol.seq
799
  [expr.call]: expr.md#expr.call
800
  [expr.const]: expr.md#expr.const
801
  [expr.dynamic.cast]: expr.md#expr.dynamic.cast
802
  [expr.new]: expr.md#expr.new
 
803
  [expr.throw]: expr.md#expr.throw
804
  [expr.typeid]: expr.md#expr.typeid
805
  [futures]: thread.md#futures
806
  [intro.execution]: basic.md#intro.execution
807
  [intro.races]: basic.md#intro.races
808
  [over.binary]: over.md#over.binary
809
  [over.match]: over.md#over.match
810
  [over.over]: over.md#over.over
811
  [propagation]: support.md#propagation
812
  [stmt.jump]: stmt.md#stmt.jump
 
813
  [stmt.pre]: stmt.md#stmt.pre
814
  [stmt.return]: stmt.md#stmt.return
815
  [structure.specifications]: library.md#structure.specifications
816
  [support.start.term]: support.md#support.start.term
817
  [temp.explicit]: temp.md#temp.explicit
 
818
  [thread.condition.condvar]: thread.md#thread.condition.condvar
819
  [thread.condition.condvarany]: thread.md#thread.condition.condvarany
820
  [thread.thread.assign]: thread.md#thread.thread.assign
821
  [thread.thread.constr]: thread.md#thread.thread.constr
822
  [thread.thread.destr]: thread.md#thread.thread.destr
 
9
  in the handler’s try block or in functions called from the handler’s try
10
  block.
11
 
12
  ``` bnf
13
  try-block:
14
+ try compound-statement handler-seq
15
  ```
16
 
17
  ``` bnf
18
  function-try-block:
19
+ try ctor-initializerₒₚₜ compound-statement handler-seq
20
  ```
21
 
22
  ``` bnf
23
  handler-seq:
24
  handler handler-seqₒₚₜ
25
  ```
26
 
27
  ``` bnf
28
  handler:
29
+ catch '(' exception-declaration ')' compound-statement
30
  ```
31
 
32
  ``` bnf
33
  exception-declaration:
34
  attribute-specifier-seqₒₚₜ type-specifier-seq declarator
 
42
  A *try-block* is a *statement* [[stmt.pre]].
43
 
44
  [*Note 1*: Within this Clause “try block” is taken to mean both
45
  *try-block* and *function-try-block*. — *end note*]
46
 
47
+ The *compound-statement* of a try block or of a handler is a
48
+ control-flow-limited statement [[stmt.label]].
49
 
50
  [*Example 1*:
51
 
52
  ``` cpp
53
  void f() {
 
183
  handler with a matching type [[except.handle]]; “nearest” means the
184
  handler for which the *compound-statement* or *ctor-initializer*
185
  following the `try` keyword was most recently entered by the thread of
186
  control and not yet exited.
187
 
188
+ Throwing an exception copy-initializes [[dcl.init]], [[class.copy.ctor]]
189
+ a temporary object, called the *exception object*. If the type of the
 
 
190
  exception object would be an incomplete type, an abstract class type
191
  [[class.abstract]], or a pointer to an incomplete type other than
192
  cv `void` the program is ill-formed.
193
 
194
  The memory for the exception object is allocated in an unspecified way,
 
231
  [*Note 4*: An exception can have active handlers and still be
232
  considered uncaught if it is rethrown. — *end note*]
233
 
234
  If the exception handling mechanism handling an uncaught exception
235
  [[except.uncaught]] directly invokes a function that exits via an
236
+ exception, the function `std::terminate` is invoked
237
+ [[except.terminate]].
238
 
239
  [*Example 2*:
240
 
241
  ``` cpp
242
  struct C {
 
256
  }
257
  ```
258
 
259
  — *end example*]
260
 
261
+ [*Note 5*: If a destructor directly invoked by stack unwinding exits
262
+ via an exception, `std::terminate` is invoked. — *end note*]
 
 
 
 
263
 
264
  ## Constructors and destructors <a id="except.ctor">[[except.ctor]]</a>
265
 
266
  As control passes from the point where an exception is thrown to a
267
+ handler, objects are destroyed by a process, specified in this
268
+ subclause, called *stack unwinding*.
269
 
270
  Each object with automatic storage duration is destroyed if it has been
271
  constructed, but not yet destroyed, since the try block was entered. If
272
  an exception is thrown during the destruction of temporaries or local
273
  variables for a `return` statement [[stmt.return]], the destructor for
 
299
  returned object, followed by the destruction of the local variable `a`.
300
  Finally, the returned object is constructed again at \#2.
301
 
302
  — *end example*]
303
 
304
+ If the initialization of an object other than by delegating constructor
305
+ is terminated by an exception, the destructor is invoked for each of the
306
+ object’s subobjects that were known to be initialized by the object’s
307
+ initialization and whose initialization has completed [[dcl.init]].
 
 
 
308
 
309
  [*Note 1*: If such an object has a reference member that extends the
310
  lifetime of a temporary object, this ends the lifetime of the reference
311
  member, so the lifetime of the temporary object is effectively not
312
  extended. — *end note*]
313
 
314
+ A subobject is *known to be initialized* if its initialization is
315
+ specified
316
+
317
+ - in [[class.base.init]] for initialization by constructor,
318
+ - in [[class.copy.ctor]] for initialization by defaulted copy/move
319
+ constructor,
320
+ - in [[class.inhctor.init]] for initialization by inherited constructor,
321
+ - in [[dcl.init.aggr]] for aggregate initialization,
322
+ - in [[expr.prim.lambda.capture]] for the initialization of the closure
323
+ object when evaluating a *lambda-expression*,
324
+ - in [[dcl.init.general]] for default-initialization,
325
+ value-initialization, or direct-initialization of an array.
326
+
327
+ [*Note 2*: This includes virtual base class subobjects if the
328
+ initialization is for a complete object, and can include variant members
329
+ that were nominated explicitly by a *mem-initializer* or
330
+ *designated-initializer-clause* or that have a default member
331
+ initializer. — *end note*]
332
+
333
+ If the destructor of an object is terminated by an exception, each
334
+ destructor invocation that would be performed after executing the body
335
+ of the destructor [[class.dtor]] and that has not yet begun execution is
336
+ performed.
337
+
338
+ [*Note 3*: This includes virtual base class subobjects if the
339
+ destructor was invoked for a complete object. — *end note*]
340
+
341
  The subobjects are destroyed in the reverse order of the completion of
342
  their construction. Such destruction is sequenced before entering a
343
  handler of the *function-try-block* of the constructor or destructor, if
344
  any.
345
 
 
347
  constructor for an object exits via an exception, the object’s
348
  destructor is invoked. Such destruction is sequenced before entering a
349
  handler of the *function-try-block* of a delegating constructor for that
350
  object, if any.
351
 
352
+ [*Note 4*: If the object was allocated by a *new-expression*
353
  [[expr.new]], the matching deallocation function
354
  [[basic.stc.dynamic.deallocation]], if any, is called to free the
355
  storage occupied by the object. — *end note*]
356
 
357
  ## Handling an exception <a id="except.handle">[[except.handle]]</a>
 
359
  The *exception-declaration* in a *handler* describes the type(s) of
360
  exceptions that can cause that *handler* to be entered. The
361
  *exception-declaration* shall not denote an incomplete type, an abstract
362
  class type, or an rvalue reference type. The *exception-declaration*
363
  shall not denote a pointer or reference to an incomplete type, other
364
+ than “pointer to cv `void`”.
 
365
 
366
  A handler of type “array of `T`” or function type `T` is adjusted to be
367
  of type “pointer to `T`”.
368
 
369
  A *handler* is a match for an exception object of type `E` if
 
441
 
442
  The exception with the most recently activated handler that is still
443
  active is called the *currently handled exception*.
444
 
445
  If no matching handler is found, the function `std::terminate` is
446
+ invoked; whether or not the stack is unwound before this invocation of
447
  `std::terminate` is *implementation-defined* [[except.terminate]].
448
 
449
  Referring to any non-static member or base class of an object in the
450
  handler for a *function-try-block* of a constructor or destructor for
451
  that object results in undefined behavior.
452
 
 
 
 
453
  Exceptions thrown in destructors of objects with static storage duration
454
+ or in constructors of objects associated with non-block variables with
455
+ static storage duration are not caught by a *function-try-block* on the
456
+ `main` function [[basic.start.main]]. Exceptions thrown in destructors
457
+ of objects with thread storage duration or in constructors of objects
458
+ associated with non-block variables with thread storage duration are not
459
+ caught by a *function-try-block* on the initial function of the thread.
460
 
461
  If a `return` statement [[stmt.return]] appears in a handler of the
462
  *function-try-block* of a constructor, the program is ill-formed.
463
 
464
  The currently handled exception is rethrown if control reaches the end
 
470
  The variable declared by the *exception-declaration*, of type cv `T` or
471
  cv `T&`, is initialized from the exception object, of type `E`, as
472
  follows:
473
 
474
  - if `T` is a base class of `E`, the variable is copy-initialized
475
+ [[dcl.init]] from an lvalue of type `T` designating the corresponding
476
+ base class subobject of the exception object;
477
+ - otherwise, the variable is copy-initialized [[dcl.init]] from an
478
+ lvalue of type `E` designating the exception object.
479
 
480
  The lifetime of the variable ends when the handler exits, after the
481
  destruction of any objects with automatic storage duration initialized
482
  within the handler.
483
 
 
496
  implicitly, or defined explicitly by using a *noexcept-specifier* as a
497
  suffix of a function declarator [[dcl.fct]].
498
 
499
  ``` bnf
500
  noexcept-specifier:
501
+ noexcept '(' constant-expression ')'
502
+ noexcept
503
  ```
504
 
505
  In a *noexcept-specifier*, the *constant-expression*, if supplied, shall
506
  be a contextually converted constant expression of type `bool`
507
  [[expr.const]]; that constant expression is the exception specification
 
509
  token that follows `noexcept` is part of the *noexcept-specifier* and
510
  does not commence an initializer [[dcl.init]]. The *noexcept-specifier*
511
  `noexcept` without a *constant-expression* is equivalent to the
512
  *noexcept-specifier* `noexcept(true)`.
513
 
514
+ [*Example 1*:
515
+
516
+ ``` cpp
517
+ void f() noexcept(sizeof(char[2])); // error: narrowing conversion of value 2 to type bool
518
+ void g() noexcept(sizeof(char)); // OK, conversion of value 1 to type bool is non-narrowing
519
+ ```
520
+
521
+ — *end example*]
522
+
523
  If a declaration of a function does not have a *noexcept-specifier*, the
524
  declaration has a potentially throwing exception specification unless it
525
  is a destructor or a deallocation function or is defaulted on its first
526
  declaration, in which cases the exception specification is as specified
527
  below and no other declaration for that function shall have a
528
  *noexcept-specifier*. In an explicit instantiation [[temp.explicit]] a
529
  *noexcept-specifier* may be specified, but is not required. If a
530
+ *noexcept-specifier* is specified in an explicit instantiation, the
531
+ exception specification shall be the same as the exception specification
532
+ of all other declarations of that function. A diagnostic is required
533
+ only if the exception specifications are not the same within a single
534
+ translation unit.
535
 
536
  If a virtual function has a non-throwing exception specification, all
537
  declarations, including the definition, of any function that overrides
538
  that virtual function in any derived class shall have a non-throwing
539
  exception specification, unless the overriding function is defined as
540
  deleted.
541
 
542
+ [*Example 2*:
543
 
544
  ``` cpp
545
  struct B {
546
  virtual void f() noexcept;
547
  virtual void g();
 
562
  — *end example*]
563
 
564
  Whenever an exception is thrown and the search for a handler
565
  [[except.handle]] encounters the outermost block of a function with a
566
  non-throwing exception specification, the function `std::terminate` is
567
+ invoked [[except.terminate]].
568
 
569
  [*Note 1*: An implementation is not permitted to reject an expression
570
  merely because, when executed, it throws or might throw an exception
571
  from a function with a non-throwing exception
572
  specification. — *end note*]
573
 
574
+ [*Example 3*:
575
 
576
  ``` cpp
577
  extern void f(); // potentially-throwing
578
 
579
  void g() noexcept {
580
  f(); // valid, even if f throws
581
  throw 42; // valid, effectively a call to std::terminate
582
  }
583
  ```
584
 
585
+ The call to `f` is well-formed despite the possibility for it to throw
586
  an exception.
587
 
588
  — *end example*]
589
 
590
  An expression E is *potentially-throwing* if
 
593
  function type, or a pointer-to-function type, with a
594
  potentially-throwing exception specification, or
595
  - E implicitly invokes a function (such as an overloaded operator, an
596
  allocation function in a *new-expression*, a constructor for a
597
  function argument, or a destructor if E is a full-expression
598
+ [[intro.execution]]) that has a potentially-throwing exception
599
+ specification, or
600
  - E is a *throw-expression* [[expr.throw]], or
601
  - E is a `dynamic_cast` expression that casts to a reference type and
602
  requires a runtime check [[expr.dynamic.cast]], or
603
  - E is a `typeid` expression applied to a (possibly parenthesized)
604
  built-in unary `*` operator applied to a pointer to a polymorphic
 
609
  An implicitly-declared constructor for a class `X`, or a constructor
610
  without a *noexcept-specifier* that is defaulted on its first
611
  declaration, has a potentially-throwing exception specification if and
612
  only if any of the following constructs is potentially-throwing:
613
 
614
+ - the invocation of a constructor selected by overload resolution in the
615
+ implicit definition of the constructor for class `X` to initialize a
616
  potentially constructed subobject, or
617
  - a subexpression of such an initialization, such as a default argument
618
  expression, or,
619
  - for a default constructor, a default member initializer.
620
 
621
  [*Note 2*: Even though destructors for fully-constructed subobjects are
622
  invoked when an exception is thrown during the execution of a
623
  constructor [[except.ctor]], their exception specifications do not
624
  contribute to the exception specification of the constructor, because an
625
  exception thrown from such a destructor would call the function
626
+ `std::terminate` rather than escape the constructor
627
+ [[except.throw]], [[except.terminate]]. — *end note*]
628
 
629
  The exception specification for an implicitly-declared destructor, or a
630
  destructor without a *noexcept-specifier*, is potentially-throwing if
631
  and only if any of the destructors for any of its potentially
632
+ constructed subobjects has a potentially-throwing exception
633
+ specification or the destructor is virtual and the destructor of any
634
+ virtual base class has a potentially-throwing exception specification.
635
 
636
  The exception specification for an implicitly-declared assignment
637
  operator, or an assignment-operator without a *noexcept-specifier* that
638
  is defaulted on its first declaration, is potentially-throwing if and
639
  only if the invocation of any assignment operator in the implicit
 
646
  The exception specification for a comparison operator function
647
  [[over.binary]] without a *noexcept-specifier* that is defaulted on its
648
  first declaration is potentially-throwing if and only if any expression
649
  in the implicit definition is potentially-throwing.
650
 
651
+ [*Example 4*:
652
 
653
  ``` cpp
654
  struct A {
655
  A(int = (A(5), 0)) noexcept;
656
  A(const A&) noexcept;
 
680
 
681
  — *end example*]
682
 
683
  An exception specification is considered to be *needed* when:
684
 
685
+ - in an expression, the function is selected by overload resolution
686
+ [[over.match]], [[over.over]];
687
+ - the function is odr-used [[term.odr.use]] or, if it appears in an
 
688
  unevaluated operand, would be odr-used if the expression were
689
  potentially-evaluated;
690
  - the exception specification is compared to that of another declaration
691
  (e.g., an explicit specialization or an overriding virtual function);
692
  - the function is defined; or
 
702
  a specialization of a function template or member function of a class
703
  template is instantiated only when needed.
704
 
705
  ## Special functions <a id="except.special">[[except.special]]</a>
706
 
707
+ ### General <a id="except.special.general">[[except.special.general]]</a>
708
+
709
  The function `std::terminate` [[except.terminate]] is used by the
710
  exception handling mechanism for coping with errors related to the
711
  exception handling mechanism itself. The function
712
  `std::current_exception()` [[propagation]] and the class
713
  `std::nested_exception` [[except.nested]] can be used by a program to
714
  capture the currently handled exception.
715
 
716
  ### The `std::terminate` function <a id="except.terminate">[[except.terminate]]</a>
717
 
718
+ In some situations exception handling is abandoned for less subtle error
719
+ handling techniques.
720
 
721
  [*Note 1*:
722
 
723
  These situations are:
724
 
 
731
  - when the search for a handler [[except.handle]] encounters the
732
  outermost block of a function with a non-throwing exception
733
  specification [[except.spec]], or
734
  - when the destruction of an object during stack unwinding
735
  [[except.ctor]] terminates by throwing an exception, or
736
+ - when initialization of a non-block variable with static or thread
737
  storage duration [[basic.start.dynamic]] exits via an exception, or
738
  - when destruction of an object with static or thread storage duration
739
  exits via an exception [[basic.start.term]], or
740
  - when execution of a function registered with `std::atexit` or
741
  `std::at_quick_exit` exits via an exception [[support.start.term]], or
 
745
  - when the function `std::nested_exception::rethrow_nested` is called
746
  for an object that has captured no exception [[except.nested]], or
747
  - when execution of the initial function of a thread exits via an
748
  exception [[thread.thread.constr]], or
749
  - for a parallel algorithm whose `ExecutionPolicy` specifies such
750
+ behavior [[execpol.seq]], [[execpol.par]], [[execpol.parunseq]], when
751
+ execution of an element access function [[algorithms.parallel.defns]]
752
+ of the parallel algorithm exits via an exception
753
+ [[algorithms.parallel.exceptions]], or
754
  - when the destructor or the move assignment operator is invoked on an
755
+ object of type `std::thread` that refers to a joinable thread
756
+ [[thread.thread.destr]], [[thread.thread.assign]], or
757
  - when a call to a `wait()`, `wait_until()`, or `wait_for()` function on
758
+ a condition variable
759
+ [[thread.condition.condvar]], [[thread.condition.condvarany]] fails to
760
+ meet a postcondition.
761
 
762
  — *end note*]
763
 
764
+ In such cases, the function `std::terminate` is invoked
765
  [[exception.terminate]]. In the situation where no matching handler is
766
  found, it is *implementation-defined* whether or not the stack is
767
+ unwound before `std::terminate` is invoked. In the situation where the
768
  search for a handler [[except.handle]] encounters the outermost block of
769
  a function with a non-throwing exception specification [[except.spec]],
770
  it is *implementation-defined* whether the stack is unwound, unwound
771
  partially, or not unwound at all before the function `std::terminate` is
772
+ invoked. In all other situations, the stack shall not be unwound before
773
+ the function `std::terminate` is invoked. An implementation is not
774
  permitted to finish stack unwinding prematurely based on a determination
775
+ that the unwind process will eventually cause an invocation of the
776
+ function `std::terminate`.
777
 
778
+ ### The `std::uncaught_exceptions` function <a id="except.uncaught">[[except.uncaught]]</a>
779
 
780
  An exception is considered uncaught after completing the initialization
781
  of the exception object [[except.throw]] until completing the activation
782
  of a handler for the exception [[except.handle]].
783
 
784
  [*Note 1*: As a consequence, an exception is considered uncaught during
785
  any stack unwinding resulting from it being thrown. — *end note*]
786
 
787
+ If an exception is rethrown [[expr.throw]], [[propagation]], it is
788
  considered uncaught from the point of rethrow until the rethrown
789
+ exception is caught. The function `std::uncaught_exceptions`
790
  [[uncaught.exceptions]] returns the number of uncaught exceptions in the
791
  current thread.
792
 
793
  <!-- Link reference definitions -->
794
  [algorithms.parallel.defns]: algorithms.md#algorithms.parallel.defns
795
  [algorithms.parallel.exceptions]: algorithms.md#algorithms.parallel.exceptions
 
 
796
  [basic.start.dynamic]: basic.md#basic.start.dynamic
797
  [basic.start.main]: basic.md#basic.start.main
798
  [basic.start.term]: basic.md#basic.start.term
799
  [basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
800
  [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
801
  [class.abstract]: class.md#class.abstract
802
+ [class.base.init]: class.md#class.base.init
803
  [class.copy.ctor]: class.md#class.copy.ctor
804
  [class.copy.elision]: class.md#class.copy.elision
805
  [class.dtor]: class.md#class.dtor
806
+ [class.inhctor.init]: class.md#class.inhctor.init
807
  [conv.fctptr]: expr.md#conv.fctptr
808
  [conv.ptr]: expr.md#conv.ptr
809
  [conv.qual]: expr.md#conv.qual
810
  [dcl.fct]: dcl.md#dcl.fct
811
  [dcl.init]: dcl.md#dcl.init
812
+ [dcl.init.aggr]: dcl.md#dcl.init.aggr
813
+ [dcl.init.general]: dcl.md#dcl.init.general
814
  [except]: #except
815
  [except.ctor]: #except.ctor
816
  [except.handle]: #except.handle
817
  [except.nested]: support.md#except.nested
818
  [except.pre]: #except.pre
819
  [except.spec]: #except.spec
820
  [except.special]: #except.special
821
+ [except.special.general]: #except.special.general
822
  [except.terminate]: #except.terminate
823
  [except.throw]: #except.throw
824
  [except.uncaught]: #except.uncaught
825
  [exception.terminate]: support.md#exception.terminate
826
  [execpol.par]: utilities.md#execpol.par
 
828
  [execpol.seq]: utilities.md#execpol.seq
829
  [expr.call]: expr.md#expr.call
830
  [expr.const]: expr.md#expr.const
831
  [expr.dynamic.cast]: expr.md#expr.dynamic.cast
832
  [expr.new]: expr.md#expr.new
833
+ [expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
834
  [expr.throw]: expr.md#expr.throw
835
  [expr.typeid]: expr.md#expr.typeid
836
  [futures]: thread.md#futures
837
  [intro.execution]: basic.md#intro.execution
838
  [intro.races]: basic.md#intro.races
839
  [over.binary]: over.md#over.binary
840
  [over.match]: over.md#over.match
841
  [over.over]: over.md#over.over
842
  [propagation]: support.md#propagation
843
  [stmt.jump]: stmt.md#stmt.jump
844
+ [stmt.label]: stmt.md#stmt.label
845
  [stmt.pre]: stmt.md#stmt.pre
846
  [stmt.return]: stmt.md#stmt.return
847
  [structure.specifications]: library.md#structure.specifications
848
  [support.start.term]: support.md#support.start.term
849
  [temp.explicit]: temp.md#temp.explicit
850
+ [term.odr.use]: basic.md#term.odr.use
851
  [thread.condition.condvar]: thread.md#thread.condition.condvar
852
  [thread.condition.condvarany]: thread.md#thread.condition.condvarany
853
  [thread.thread.assign]: thread.md#thread.thread.assign
854
  [thread.thread.constr]: thread.md#thread.thread.constr
855
  [thread.thread.destr]: thread.md#thread.thread.destr