From cppreference.com
An
thrown in a try block can possibly be handled by an associated handler.
Syntax
trycompound-statementhandler-seq(1) tryctor-initializer(optional)compound-statementhandler-seq(2) 1) An
.
2) A
. compound-statement must be the compound statement component of a function body.
Ordinary try block
An ordinary try block is a
.
If an exception is thrown from its compound-statement, the exception will be matched against the
in its handler-seq :
voidf(){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}Function try block
A function try block is a special kind of
.
If an exception is thrown from its compound-statement or ctor-initializer (if any), the exception will be matched against the
in its handler-seq :
intf(boolcond){if(cond)throw1;return0;}structX{intmem;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 static
or in constructors of objects associated with
with static storage duration are not caught by a function try block on the
.
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 function try block on the initial function of the thread.
(since C++11)Flowing off the end of the compound-statement of a
of a function try block is equivalent to flowing off the end of the compound-statement of that function try block, unless the function is a constructor or destructor (see below).
Constructor and destructor try block
For a class C, if the function body of its constuctor or destructor definition is a function try block, and an exception is thrown during the initialization or destruction, respectively, of C’s subobjects, the exception will also be matched against the
in the handler-seq of the function try block:
intf(boolcond=true){if(cond)throw1;return0;}structX{intmem=f();~X(){throw2;}};structY{Xmem;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 function try block of a constructor or destructor for that object results in undefined behavior.
If a
appears in a handler of the function try block of a constructor, the program is ill-formed.
The
is rethrown if control reaches the end of a handler of the function try block of a constructor or destructor.
Control flow
The compound-statement of a try block is a
control-flow-limited statement
:
voidf(){gotolabel;// errortry{gotolabel;// OKlabel:;}catch(...){gotolabel;// error}}A
(
,
,
,
) can be used to transfer control out of a try block (including its handlers). When this happens, each variable declared in the try block will be destroyed in the context that directly contains its declaration:
try{T1t1;try{T2t2;gotolabel;// 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:;Keywords
Notes
Feature-test macro ValueStdFeature
(C++20)try blocks in constexpr functions Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 a switch statement can transfer control
into the compound-statement of a try block prohibited
C++98 it was unspecified whether a function try block on a destructor
will catch exceptions from a base or member destructor such exceptions
are caught See also