[except.pre] (original) (raw)

14 Exception handling [except]

14.1 Preamble [except.pre]

[ Note

:

Within this Clause “try block” is taken to mean both try-block andfunction-try-block.

end note

]

A goto or switch statement shall not be used to transfer control into a try block or into a handler.

[ Example

:

void f() { goto l1; // error goto l2; // error try { goto l1; // OK goto l2; // error l1: ; } catch (...) { l2: ; goto l1; // error goto l2; // OK } }

end example

]

Agoto,break,return, orcontinuestatement 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.

[ Example

:

lab: try { T1 t1; try { T2 t2; if (condition) goto lab; } catch(...) { /* handler 2 / } } catch(...) { / handler 1 */ }

Here, executinggoto lab;will destroy firstt2, thent1, assuming theconditiondoes not declare a variable.

Any exception thrown while destroyingt2will result in executinghandler 2; any exception thrown while destroyingt1will result in executinghandler 1.

end example

]

An exception thrown during the execution of thecompound-statementor, for constructors and destructors, during the initialization or destruction, respectively, of the class's subobjects, transfers control to a handler in afunction-try-blockin the same way as an exception thrown during the execution of atry-blocktransfers control to other handlers.

[ Example

:

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 example

]