[except.pre] (original) (raw)

14 Exception handling [except]

14.1 Preamble [except.pre]

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 throwing an exception in code executed in the handler's try block or in functions called from the handler's try block.

[Note 1:

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

— _end note_]

[Example 1: void f() { goto l1; goto l2; try { goto l1; goto l2; l1: ;} catch (...) { l2: ;goto l1; goto l2; } } — _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 2: lab: try { T1 t1;try { T2 t2;if (condition) goto lab;} catch(...) { } } catch(...) { }

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 3: 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) { } catch (...) { } — _end example_]