forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.tex
More file actions
1299 lines (1170 loc) · 38 KB
/
exceptions.tex
File metadata and controls
1299 lines (1170 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\rSec0[except]{Exception handling}%
\indextext{exception handling|(}
%gram: \rSec1[gram.except]{Exception handling}
%gram:
\indextext{exception object|see{exception handling, exception object}}%
\indextext{object, exception|see{exception handling, exception object}}
\pnum
Exception handling provides a way of transferring control and information
from a point in the execution of a thread to an exception handler
associated with a point previously passed by the execution.
A handler will be invoked only by a
\grammarterm{throw-expression}
invoked in code executed in the handler's try block
or in functions called from the handler's try block .
\indextext{\idxcode{try}}%
%
\begin{bnf}
\nontermdef{try-block}\br
\terminal{try} compound-statement handler-seq
\end{bnf}
\indextext{\idxcode{try}}%
%
\begin{bnf}
\nontermdef{function-try-block}\br
\terminal{try} ctor-initializer\opt compound-statement handler-seq
\end{bnf}
\begin{bnf}
\nontermdef{handler-seq}\br
handler handler-seq\opt
\end{bnf}
\indextext{\idxcode{catch}}%
%
\begin{bnf}
\nontermdef{handler}\br
\terminal{catch (} exception-declaration \terminal{)} compound-statement
\end{bnf}
\begin{bnf}
\nontermdef{exception-declaration}\br
attribute-specifier-seq\opt type-specifier-seq declarator\br
attribute-specifier-seq\opt type-specifier-seq abstract-declarator\opt\br
\terminal{...}
\end{bnf}
\indextext{\idxcode{throw}}%
%
\begin{bnf}
\nontermdef{throw-expression}\br
\terminal{throw} assignment-expression\opt
\end{bnf}
The optional \grammarterm{attribute-specifier-seq} in an \grammarterm{exception-declaration}
appertains to the formal parameter of the catch clause~(\ref{except.handle}).
\pnum
\indextext{exception handling!try block}%
\indextext{exception handling!handler}%
\indextext{try block|see{exception handling, try block}}%
\indextext{handler|see{exception handling, handler}}%
A \grammarterm{try-block} is a \grammarterm{statement} (Clause~\ref{stmt.stmt}).
A \grammarterm{throw-expression} is of type \tcode{void}. Code that executes a
\grammarterm{throw-expression} is said to ``throw an exception;'' code that
subsequently gets control is called a ``handler.'' \enternote Within this Clause
``try block'' is taken to mean both \grammarterm{try-block} and
\grammarterm{function-try-block}. \exitnote
\pnum
\indextext{exception handling!\idxcode{goto}}%
\indextext{exception handling!\idxcode{switch}}%
\indextext{\idxcode{goto}!and try block}%
\indextext{\idxcode{switch}!and try block}%
\indextext{\idxcode{goto}!and handler}%
\indextext{\idxcode{switch}!and handler}%
A \tcode{goto} or \tcode{switch} statement shall not be used to transfer control
into a try block or into a handler.
\enterexample
\begin{codeblock}
void f() {
goto l1; // Ill-formed
goto l2; // Ill-formed
try {
goto l1; // OK
goto l2; // Ill-formed
l1: ;
} catch (...) {
l2: ;
goto l1; // Ill-formed
goto l2; // OK
}
}
\end{codeblock}
\exitexample
\indextext{\idxcode{goto}!and try block}%
\indextext{\idxcode{switch}!and try block}%
\indextext{\idxcode{return}!and try block}%
\indextext{\idxcode{continue}!and try block}%
\indextext{\idxcode{goto}!and handler}%
\indextext{\idxcode{switch}!and handler}%
\indextext{\idxcode{return}!and handler}%
\indextext{\idxcode{continue}!and handler}%
A
\tcode{goto},
\tcode{break},
\tcode{return},
or
\tcode{continue}
statement can be used to transfer control out of
a try block or handler.
When this happens, each variable declared in the try block
will be destroyed in the context that
directly contains its declaration.
\enterexample
\begin{codeblock}
lab: try {
T1 t1;
try {
T2 t2;
if (@\textit{condition}@)
goto lab;
} catch(...) { /* @\textit{handler 2}@ */ }
} catch(...) { /* @\textit{handler 1}@ */ }
\end{codeblock}
Here, executing
\tcode{goto lab;}
will destroy first
\tcode{t2},
then
\tcode{t1},
assuming the
\grammarterm{condition}
does not declare a variable.
Any exception raised while destroying
\tcode{t2}
will result in executing
\textit{handler 2};
any exception raised while destroying
\tcode{t1}
will result in executing
\textit{handler 1}.
\exitexample
\pnum
\indextext{function try block|see{exception handling, function try block}}%
\indextext{exception handling!function try block}%
A
\grammarterm{function-try-block}
associates a
\grammarterm{handler-seq}
with the
\grammarterm{ctor-initializer},
if present, and the
\grammarterm{compound-statement}.
An exception
thrown during the execution of the
\grammarterm{compound-statement}
or, for constructors and destructors, during the initialization or
destruction, respectively, of the class's subobjects,
transfers control to a handler in a
\grammarterm{function-try-block}
in the same way as an exception thrown during the execution of a
\grammarterm{try-block}
transfers control to other handlers.
\enterexample
\begin{codeblock}
int f(int);
class C {
int i;
double d;
public:
C(int, double);
};
C::C(int ii, double id)
try : i(f(ii)), d(id) {
// constructor statements
}
catch (...) {
// handles exceptions thrown from the ctor-initializer
// and from the constructor statements
}
\end{codeblock}
\exitexample
\rSec1[except.throw]{Throwing an exception}%
\indextext{exception handling!throwing}%
\indextext{throwing|see{exception handling, throwing}}
\pnum
Throwing an exception transfers control to a handler.
An object is passed and the type of that object determines which handlers
can catch it.
\enterexample
\begin{codeblock}
throw "Help!";
\end{codeblock}
can be caught by a
\term{handler}
of
\tcode{const}
\tcode{char*}
type:
\begin{codeblock}
try {
// ...
}
catch(const char* p) {
// handle character string exceptions here
}
\end{codeblock}
and
\begin{codeblock}
class Overflow {
public:
Overflow(char,double,double);
};
void f(double x) {
throw Overflow('+',x,3.45e107);
}
\end{codeblock}
can be caught by a handler for exceptions of type
\tcode{Overflow}
\begin{codeblock}
try {
f(1.2);
} catch(Overflow& oo) {
// handle exceptions of type \tcode{Overflow} here
}
\end{codeblock}
\exitexample
\pnum
\indextext{exception handling!throwing}%
\indextext{exception handling!handler}%
\indextext{exception handling!nearest handler}%
When an exception is thrown, control is transferred to the nearest handler with
a matching type~(\ref{except.handle}); ``nearest'' means the handler
for which the
\grammarterm{compound-statement} or
\grammarterm{ctor-initializer}
following the
\tcode{try}
keyword was most recently entered by the thread of control and not yet exited.
\pnum
A
\grammarterm{throw-expression}
initializes a temporary object,
called the
\indextext{exception handling!exception object}\term{exception object},
the type of which
is determined by removing any top-level
\grammarterm{cv-qualifier}{s}
from the static type of the operand of
\tcode{throw}
and adjusting the type from ``array of
\tcode{T}''
or ``function returning
\tcode{T}''
to ``pointer to
\tcode{T}''
or ``pointer to function
returning
\tcode{T}'',
respectively.
The temporary is an lvalue and is used to initialize the
variable named in the matching
\term{handler}~(\ref{except.handle}).
If the type of the exception object would
be an incomplete type or a pointer to an incomplete
type other than (possibly cv-qualified)
\tcode{void} the program is ill-formed.
Except for these restrictions and the restrictions on type matching mentioned
in~\ref{except.handle}, the operand of
\tcode{throw}
is treated exactly as a function argument in a call~(\ref{expr.call}) or the operand
of a return statement.
\pnum
\indextext{exception handling!memory}%
\indextext{exception handling!rethrowing}%
\indextext{exception handling!exception object}%
The memory for the exception object is
allocated in an unspecified way, except as noted in~\ref{basic.stc.dynamic.allocation}.
If a handler exits by rethrowing, control is passed to another handler for
the same exception.
The exception object is destroyed after either
the last remaining active handler for the exception exits by
any means other than
rethrowing, or the last object of type \tcode{std::exception_ptr}~(\ref{propagation})
that refers to the exception object is destroyed, whichever is later. In the former
case, the destruction occurs when the handler exits, immediately after the destruction
of the object declared in the \grammarterm{exception-declaration} in the handler, if any.
In the latter case, the destruction occurs before the destructor of \tcode{std::exception_ptr}
returns.
The implementation may then
deallocate the memory for the exception object; any such deallocation
is done in an unspecified way.
\enternote an exception thrown by a \grammarterm{throw-expression} does not
propagate to other threads unless caught, stored, and rethrown using
appropriate library functions; see~\ref{propagation} and~\ref{futures}. \exitnote
\pnum
\indextext{exception handling!exception object!constructor}%
\indextext{exception handling!exception object!destructor}%
When the thrown object is a class object, the copy/move constructor and the
destructor shall be accessible, even if the copy/move operation is
elided~(\ref{class.copy}).
\pnum
\indextext{exception handling!rethrow}%
\indextext{rethrow|see{exception handling, rethrow}}%
\indextext{reraise|see{exception handling, rethrow}}%
An exception is considered caught when a handler for that exception
becomes active~(\ref{except.handle}).
\enternote
An exception can have active handlers and still be considered uncaught if
it is rethrown.
\exitnote
\pnum
\indextext{exception handling!terminate called@\tcode{terminate()} called}%
\indextext{\idxcode{terminate()}!called}%
If the exception handling mechanism, after completing evaluation of the expression
to be thrown but before the exception is caught, calls a function that exits via an
exception, \tcode{std::terminate} is called~(\ref{except.terminate}). \enterexample
\begin{codeblock}
struct C {
C() { }
C(const C&) { throw 0; }
};
int main() {
try {
throw C(); // calls \tcode{std::terminate()}
} catch(C) { }
}
\end{codeblock}
\exitexample
\pnum
\indextext{exception handling!rethrow}%
A
\grammarterm{throw-expression}
with no operand rethrows the currently handled exception~(\ref{except.handle}).
The exception is reactivated with the existing temporary;
no new temporary exception object is created.
The exception
is no longer considered to be caught; therefore, the value
of
\tcode{std::uncaught_exception()}
will again be
\tcode{true}.
\enterexample
code that must be executed because of an exception yet cannot
completely handle the exception can be written like this:
\begin{codeblock}
try {
// ...
} catch (...) { // catch all exceptions
// respond (partially) to exception
throw; // pass the exception to some
// other handler
}
\end{codeblock}
\exitexample
\pnum
\indextext{exception handling!rethrow}%
\indextext{exception handling!terminate called@\tcode{terminate()} called}%
\indextext{\idxcode{terminate()}!called}%
If no exception is presently being handled,
executing a
\grammarterm{throw-expression}
with no operand calls
\tcode{std\colcol{}terminate()}~(\ref{except.terminate}).
\rSec1[except.ctor]{Constructors and destructors}%
\indextext{exception handling!constructors and destructors}%
\indextext{stack unwinding!see exception handling, constructors and destructors}%
\indextext{constructor!exception~handling|see{exception handling, constructors and destructors}}%
\indextext{destructor!exception~handling|see{exception handling, constructors and destructors}}
\pnum
As control passes from a
\grammarterm{throw-expression}
to a handler,
destructors are invoked for all automatic objects constructed since the
try block was entered.
The automatic objects are destroyed in the reverse order of the completion
of their construction.
\pnum
An object
of any storage duration whose initialization or destruction is terminated by an exception
will have
destructors executed for all of its fully constructed
subobjects (excluding the variant members of a union-like class),
that is, for subobjects for which the principal
constructor~(\ref{class.base.init}) has completed execution
and the destructor has not yet begun execution.
Similarly, if the non-delegating constructor for an object has
completed execution and a delegating constructor for that object exits with
an exception, the object's destructor will be invoked.
If the object was allocated in a
\grammarterm{new-expression},
the matching deallocation function~(\ref{basic.stc.dynamic.deallocation}, \ref{expr.new}, \ref{class.free}),
if any, is called to free the storage occupied by the
object.
\pnum
\indextext{unwinding!stack}%
The process of calling destructors for automatic objects constructed on the
path from a try block to a
\grammarterm{throw-expression}
is called
``\term{stack unwinding}.''
If a destructor called during stack unwinding exits with an exception,
\tcode{std\-::\-ter\-min\-ate}
is called~(\ref{except.terminate}).
\enternote
So destructors should generally catch
exceptions and not let them propagate out of the destructor.
\exitnote
\rSec1[except.handle]{Handling an exception}
\indextext{exception handling!handler|(}%
\pnum
The
\grammarterm{exception-declaration}
in a
\term{handler}
describes the type(s) of exceptions that can cause
that
\term{handler}
to be entered.
\indextext{exception handling!handler!incomplete type in}%
\indextext{exception handling!handler!rvalue reference in}%
\indextext{exception handling!handler!array in}%
\indextext{exception handling!handler!pointer to function in}%
The
\grammarterm{exception-declaration}
shall not denote an incomplete type, an abstract class type, or an rvalue reference type.
The
\grammarterm{exception-declaration}
shall not denote a pointer or reference to an
incomplete type, other than
\tcode{void*},
\tcode{const}
\tcode{void*},
\tcode{volatile}
\tcode{void*},
or
\tcode{const}
\tcode{volatile}
\tcode{void*}.
\pnum
A handler of type ``array of
\tcode{T}''
or ``function returning
\tcode{T}''
is adjusted to be of type ``pointer to
\tcode{T}''
or ``pointer to function
returning
\tcode{T}'',
respectively.
\pnum
\indextext{exception handling!handler!match|(}%
A
\term{handler}
is a match for
an exception object
of type
\tcode{E}
if
\begin{itemize}
\item%
The
\term{handler}
is of type
\textit{cv}
\tcode{T}
or
\textit{cv}
\tcode{T\&}
and
\tcode{E}
and
\tcode{T}
are the same type (ignoring the top-level
\grammarterm{cv-qualifiers}),
or
\item%
the
\term{handler}
is of type
\textit{cv}
\tcode{T}
or
\textit{cv}
\tcode{T\&}
and
\tcode{T}
is an unambiguous public base class of
\tcode{E},
or
\item%
the
\term{handler}
is of type
\textit{cv1}
\tcode{T*}
\textit{cv2}
and
\tcode{E}
is a pointer type that can be
converted to the type of the
\term{handler}
by either or both of
\begin{itemize}
\item%
a standard pointer conversion~(\ref{conv.ptr}) not involving conversions
to pointers to private or protected or ambiguous classes
\item%
a qualification conversion
\end{itemize}
\item
the \term{handler} is a pointer or pointer to member type and \tcode{E} is \tcode{std::nullptr_t}.
\end{itemize}
\enternote
A
\grammarterm{throw-expression}
whose operand is an integral constant expression of integer type
that evaluates to zero does not match a handler of pointer or pointer to member type.
\exitnote
\enterexample
\begin{codeblock}
class Matherr { /* ... */ virtual void vf(); };
class Overflow: public Matherr { /* ... */ };
class Underflow: public Matherr { /* ... */ };
class Zerodivide: public Matherr { /* ... */ };
void f() {
try {
g();
} catch (Overflow oo) {
// ...
} catch (Matherr mm) {
// ...
}
}
\end{codeblock}
Here, the
\tcode{Overflow}
handler will catch exceptions of type
\tcode{Overflow}
and the
\tcode{Matherr}
handler will catch exceptions of type
\tcode{Matherr}
and of all types publicly derived from
\tcode{Matherr}
including exceptions of type
\tcode{Underflow}
and
\tcode{Zerodivide}.
\exitexample
\pnum
The handlers for a try block are tried in order of appearance.
That makes it possible to write handlers that can never be
executed, for example by placing a handler for a derived class after
a handler for a corresponding base class.
\pnum
A
\tcode{...}
in a handler's
\grammarterm{exception-declaration}
functions similarly to
\tcode{...}
in a function parameter declaration;
it specifies a match for any exception.
If present, a
\tcode{...}
handler shall be the last handler for its try block.
\pnum
If no match is found among the handlers for a try block,
the search for a matching
handler continues in a dynamically surrounding try block
of the same thread.
\pnum
A handler is considered active when initialization is complete for
the formal parameter (if any) of the catch clause.
\enternote
The stack will have been unwound at that point.
\exitnote
Also, an implicit handler is considered active when
\tcode{std::terminate()}
or
\tcode{std::unexpected()}
is entered due to a throw. A handler is no longer considered active when the
catch clause exits or when
\tcode{std::unexpected()}
exits after being entered due to a throw.
\pnum
The exception with the most recently activated handler that is
still active is called the
\term{currently handled exception}.
\pnum
If no matching handler is found,
the function
\tcode{std::terminate()}
is called;
whether or not the stack is unwound before this call to
\tcode{std::terminate()}
is \impldef{stack unwinding before call to
\tcode{std::terminate()}}~(\ref{except.terminate}).
\pnum
Referring to any non-static member or base class of an object
in the handler for a
\grammarterm{function-try-block}
of a constructor or destructor for that object results in undefined behavior.
\pnum
The fully constructed base classes and members of an object shall
be destroyed before entering the handler of a
\grammarterm{function-try-block}
of a constructor for that object.
Similarly, if a delegating constructor for an object exits
with an exception after the non-delegating constructor for that object
has completed execution, the object's destructor shall be executed before
entering the handler of a \nonterminal{function-try-block} of a
constructor for that object. The base classes and non-variant members of an object shall be destroyed before entering the handler of a \nonterminal{function-try-block} of a destructor for that object~(\ref{class.dtor}).
\pnum
The scope and lifetime of the parameters of a function or constructor
extend into the handlers of a
\grammarterm{function-try-block}.
\pnum
Exceptions thrown in destructors of objects with static storage duration or in
constructors of namespace-scope objects with static storage duration are not caught by a
\grammarterm{function-try-block}
on
\tcode{main()}. Exceptions thrown in destructors of objects with thread storage duration or in constructors of namespace-scope objects with thread storage duration are not caught by a
\grammarterm{function-try-block}
on the initial function of the thread.
\pnum
If a return statement appears in a handler of the
\grammarterm{function-try-block}
of a
constructor, the program is ill-formed.
\pnum
The currently handled exception
is rethrown if control reaches the end of a handler of the
\grammarterm{function-try-block}
of a constructor or destructor.
Otherwise, a
function returns when control reaches the end of a handler for
the
\grammarterm{function-try-block}~(\ref{stmt.return}).
Flowing off the end of a
\grammarterm{function-try-block}
is equivalent to a
\tcode{return}
with no value;
this results in undefined behavior in a value-returning function~(\ref{stmt.return}).
\pnum
If the \grammarterm{exception-declaration} specifies a name, it declares a
variable which is copy-initialized~(\ref{dcl.init}) from the exception object.
If the \grammarterm{exception-declaration} denotes an object type but
does not specify a name, a
temporary~(\ref{class.temporary}) is copy-initialized~(\ref{dcl.init}) from the
exception object.
The lifetime of the variable or temporary ends
when the handler exits, after the
destruction of any automatic objects initialized
within the handler.
\pnum
When the handler declares a non-constant object,
any changes to that object will not affect the temporary object
that was initialized by execution of the
\grammarterm{throw-expression}.
When the handler declares a reference to a non-constant object,
any changes to the referenced object are changes to the
temporary object initialized when the
\grammarterm{throw-expression}
was executed and will have effect should that object be rethrown.%
\indextext{exception handling!handler!match|)}%
\indextext{exception handling!handler|)}
\rSec1[except.spec]{Exception specifications}%
\indextext{exception specification|(}
\pnum
A function declaration lists exceptions
that its function might directly or indirectly throw
by using an
\grammarterm{exception-specification}
as a suffix of its declarator.
\begin{bnf}
\nontermdef{exception-specification}\br
dynamic-exception-specification\br
noexcept-specification
\end{bnf}
\begin{bnf}
\nontermdef{dynamic-exception-specification}\br
\terminal{throw (} type-id-list\opt \terminal{)}
\end{bnf}
\begin{bnf}
\nontermdef{type-id-list}\br
type-id \terminal{...}\opt\br
type-id-list \terminal{,} type-id \terminal{...}\opt
\end{bnf}
\begin{bnf}
\nontermdef{noexcept-specification}\br
\terminal{noexcept} \terminal{(} constant-expression \terminal{)}\br
\terminal{noexcept}
\end{bnf}
\indextext{exception specification!noexcept!constant expression and}%
In a \grammarterm{noexcept-specification}, the \grammarterm{constant-expression},
if supplied, shall be a constant expression~(\ref{expr.const}) that is contextually
converted to \tcode{bool} (Clause~\ref{conv}). A \grammarterm{noexcept-specification}
\tcode{noexcept} is equivalent to \tcode{noexcept(\brk{}true)}.
\pnum
An
\grammarterm{exception-specification}
shall appear only on a function declarator for a function type,
pointer to function type, reference to function type, or pointer to
member function type that is the top-level type of a declaration or
definition, or on such a type appearing as a parameter or return type
in a function declarator.
An
\grammarterm{exception-specification}
shall not appear in a typedef declaration or \grammarterm{alias-declaration}.
\enterexample
\begin{codeblock}
void f() throw(int); // OK
void (*fp)() throw (int); // OK
void g(void pfa() throw(int)); // OK
typedef int (*pf)() throw(int); // ill-formed
\end{codeblock}
\exitexample
\indextext{exception specification!incomplete type and}%
A type denoted in an
\grammarterm{exception-specification}
shall not denote an incomplete type.
A type denoted in an
\grammarterm{exception-specification}
shall not denote a pointer or reference to an incomplete type, other than
\tcode{void*},
\tcode{const}
\tcode{void*},
\tcode{volatile}
\tcode{void*},
or
\tcode{const}
\tcode{volatile}
\tcode{void*}.
A type \cv\ \tcode{T}, ``array of \tcode{T}'', or ``function returning \tcode{T}''
denoted in an \grammarterm{exception-specification} is adjusted to type \tcode{T},
``pointer to \tcode{T}'', or ``pointer to function returning \tcode{T}'', respectively.
\pnum
\indextext{exception specification!compatible}%
Two \grammarterm{exception-specification}{s} are
\indextext{compatible|see{exception specification, compatible}}\term{compatible} if:
\begin{itemize}
\item both are non-throwing (see below), regardless of their form,
\item both have the form \tcode{noexcept(}\grammarterm{constant-expression}{}\tcode{)}
and the \grammarterm{constant-expression}{s} are equivalent, or
\item both are \grammarterm{dynamic-exception-specification}{s} that have the same
set of adjusted types.
\end{itemize}
\pnum
If any declaration of a function has an
\grammarterm{exception-specification}
that is not a \grammarterm{noexcept-specification} allowing all exceptions,
all declarations, including the definition and any explicit specialization,
of that function shall have a compatible
\grammarterm{exception-specification}.
If any declaration of a pointer to function, reference to function,
or pointer to member function has an
\grammarterm{exception-specification},
all occurrences of that declaration shall have a compatible
\grammarterm{exception-specification}
In an explicit instantiation an
\grammarterm{exception-specification}
may be specified, but is not required.
If an
\grammarterm{exception-specification}
is specified in an explicit instantiation directive, it shall
be compatible with the \grammarterm{exception-specification}{s} of
other declarations of that function.
A diagnostic is required only if the
\grammarterm{exception-specification}{s} are not compatible
within a single translation unit.
\pnum
\indextext{exception specification!virtual function and}%
If a virtual function has an
\grammarterm{exception-specification},
all declarations, including the definition, of any function
that overrides that virtual function in any derived class
shall only allow exceptions that are allowed by the
\grammarterm{exception-specification}
of the base class virtual function.
\enterexample
\begin{codeblock}
struct B {
virtual void f() throw (int, double);
virtual void g();
};
struct D: B {
void f(); // ill-formed
void g() throw (int); // OK
};
\end{codeblock}
The declaration of
\tcode{D::f}
is ill-formed because it allows all exceptions, whereas
\tcode{B::f}
allows only
\tcode{int}
and
\tcode{double}.
\exitexample
A similar restriction applies to assignment to and
initialization of pointers to functions, pointers
to member functions, and references to functions:
the target entity shall allow at least the exceptions
allowed by the source value in the assignment or
initialization.
\enterexample
\begin{codeblock}
class A { /* ... */ };
void (*pf1)(); // no exception specification
void (*pf2)() throw(A);
void f() {
pf1 = pf2; // OK: \tcode{pf1} is less restrictive
pf2 = pf1; // error: \tcode{pf2} is more restrictive
}
\end{codeblock}
\exitexample
\pnum
In such an assignment or initialization,
\grammarterm{exception-specification}{s}
on return types and parameter types shall be compatible.
In other assignments or initializations,
\grammarterm{exception-specification}{s}
shall be compatible.
\pnum
An
\grammarterm{exception-specification}
can include the same type more than once
and can include classes that are related by inheritance,
even though doing so is redundant.
\enternote An
\grammarterm{exception-specification}
can also include the class
\tcode{std::bad_exception}~(\ref{bad.exception}).
\exitnote
\pnum
\indextext{exception handling!allowing an exception}%
\indextext{allowing an exception|see{exception handling, allowing an exception}}%
A function is said to
\term{allow}
an exception of type
\tcode{E}
if
the \grammarterm{constant-expression} in its \grammarterm{noexcept-specification}
evaluates to \tcode{false} or
its
\grammarterm{dynamic-exception-specification}
contains a type
\tcode{T}
for which a handler of type
\tcode{T}
would be a match~(\ref{except.handle}) for an exception of type
\tcode{E}.
\pnum
\indextext{exception handling!unexpected called@\tcode{unexpected()} called}%
\indextext{\idxcode{unexpected()}!called}%
Whenever an exception is thrown and the search for a handler~(\ref{except.handle})
encounters the outermost block of a function with an
\grammarterm{exception-specification} that does not allow the exception, then,
\begin{itemize}
\item if the \grammarterm{exception-specification} is a
\grammarterm{dynamic-exception-specification}, the function
\tcode{std::unexpected()} is called~(\ref{except.unexpected}),
\indextext{exception handling!terminate called@\tcode{terminate()} called}%
\indextext{\idxcode{terminate()}!called}%
\item otherwise, the function \tcode{std::terminate()} is called~(\ref{except.terminate}).
\end{itemize}
\enterexample
\begin{codeblock}
class X { };
class Y { };
class Z: public X { };
class W { };
void f() throw (X, Y) {
int n = 0;
if (n) throw X(); // OK
if (n) throw Z(); // also OK
throw W(); // will call \tcode{std::unexpected()}
}
\end{codeblock}
\exitexample
\enternote A function can have multiple declarations with different non-throwing
\grammarterm{exception-specification}{s}; for this purpose, the one on the
function definition is used. \exitnote
\pnum
\indextext{\idxcode{unexpected()}}%
The function
\tcode{unexpected()}
may throw an exception that will satisfy the
\grammarterm{exception-specification}
for which it was invoked, and in this case the search for another handler
will continue at the call of the function with this
\grammarterm{exception-specification}
(see~\ref{except.unexpected}), or it may call
\tcode{std::terminate()}.
\pnum
An implementation shall not reject an expression merely because when
executed it throws or might
throw an exception that the containing function does not allow.
\enterexample
\begin{codeblock}
extern void f() throw(X, Y);
void g() throw(X) {
f(); // OK
}
\end{codeblock}
the call to
\tcode{f}
is well-formed even though when called,
\tcode{f}