try
blockGeneral topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
| ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
try block | ||||
Throwing exceptions | ||||
Handling exceptions | ||||
Exception specification | ||||
noexcept specification(C++11) | ||||
dynamic specification(until C++17*) | ||||
noexcept operator(C++11) |
Anexception thrown in atry block can possibly be handled by an associated handler.
Contents |
try compound-statementhandler-seq | (1) | ||||||||
try ctor-initializer (optional)compound-statementhandler-seq | (2) | ||||||||
compound-statement | - | acompound statement |
handler-seq | - | a non-empty sequence ofhandlers |
ctor-initializer | - | member initializer list (forconstructors only) |
An ordinarytry block is astatement.
If an exception is thrown from itscompound-statement, the exception will be matched against thehandlers in itshandler-seq :
void f(){throw1;// NOT handled by the handler belowtry{throw2;// handled by the associated handler}catch(...){// handles the exception 2}throw3;// NOT handled by the handler above}
A functiontry block is a special kind offunction body.
If an exception is thrown from itscompound-statement orctor-initializer (if any), the exception will be matched against thehandlers in itshandler-seq :
int f(bool cond){if(cond)throw1;return0;} struct X{int mem; X()try: mem(f(true)){}catch(...){// handles the exception 1} X(int)try{throw2;}catch(...){// handles the exception 2}};
Exceptions thrown in destructors of objects with staticstorage duration or in constructors of objects associated withnon-block variables with static storage duration are not caught by a functiontry block on themain function.
Exceptions thrown in destructors of objects with thread storage duration or in constructors of objects associated with non-block variables with thread storage duration are not caught by a functiontry block on the initial function of the thread. | (since C++11) |
Flowing off the end of thecompound-statement of ahandler of a functiontry block is equivalent to flowing off the end of thecompound-statement of that functiontry block, unless the function is a constructor or destructor (see below).
For a classC
, if the function body of its constuctor or destructor definition is a functiontry block, and an exception is thrown during the initialization or destruction, respectively, ofC
’s subobjects, the exception will also be matched against thehandlers in thehandler-seq of the functiontry block:
int f(bool cond=true){if(cond)throw1;return0;} struct X{int mem= f(); ~X(){throw2;}}; struct Y{ X mem; Y()try{}catch(...){// handles the exception 1} ~Y()try{}catch(...){// handles the exception 2}};
Referring to any non-static member or base class of an object in the handler for a functiontry block of a constructor or destructor for that object results in undefined behavior.
If areturn statement appears in a handler of the functiontry block of a constructor, the program is ill-formed.
Thecurrently handled exception is rethrown if control reaches the end of a handler of the functiontry block of a constructor or destructor.
Thecompound-statement of atry block is acontrol-flow-limited statement:
void f(){goto label;// errortry{goto label;// OK label:;}catch(...){goto label;// error}}
Ajump statement (goto
,break
,return
,continue
) can be used to transfer control out of atry block (including its handlers). When this happens, each variable declared in thetry block will be destroyed in the context that directly contains its declaration:
try{ T1 t1;try{ T2 t2;goto label;// destroy t2 first, then t1}catch(...){// executed if an exception is thrown while destroying t2}}catch(...){// executed if an exception is thrown while destroying t1}label:;
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 98 | C++98 | aswitch statement can transfer control into thecompound-statement of atry block | prohibited |
CWG 1167 | C++98 | it was unspecified whether a functiontry block on a destructor will catch exceptions from a base or member destructor | such exceptions are caught |