100 lines
6.4 KiB
Markdown
100 lines
6.4 KiB
Markdown
[except.pre]
|
||
|
||
# 14 Exception handling [[except]](./#except)
|
||
|
||
## 14.1 Preamble [except.pre]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L12)
|
||
|
||
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[.](#1.sentence-1)
|
||
|
||
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[.](#1.sentence-2)
|
||
|
||
[try-block:](#nt:try-block "14.1 Preamble [except.pre]")
|
||
try [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") [*handler-seq*](#nt:handler-seq "14.1 Preamble [except.pre]")
|
||
|
||
[function-try-block:](#nt:function-try-block "14.1 Preamble [except.pre]")
|
||
try [*ctor-initializer*](class.base.init#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]")opt [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") [*handler-seq*](#nt:handler-seq "14.1 Preamble [except.pre]")
|
||
|
||
[handler-seq:](#nt:handler-seq "14.1 Preamble [except.pre]")
|
||
[*handler*](#nt:handler "14.1 Preamble [except.pre]") [*handler-seq*](#nt:handler-seq "14.1 Preamble [except.pre]")opt
|
||
|
||
[handler:](#nt:handler "14.1 Preamble [except.pre]")
|
||
catch ( [*exception-declaration*](#nt:exception-declaration "14.1 Preamble [except.pre]") ) [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]")
|
||
|
||
[exception-declaration:](#nt:exception-declaration "14.1 Preamble [except.pre]")
|
||
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt [*type-specifier-seq*](dcl.type.general#nt:type-specifier-seq "9.2.9.1 General [dcl.type.general]") [*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]")
|
||
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt [*type-specifier-seq*](dcl.type.general#nt:type-specifier-seq "9.2.9.1 General [dcl.type.general]") [*abstract-declarator*](dcl.name#nt:abstract-declarator "9.3.2 Type names [dcl.name]")opt
|
||
...
|
||
|
||
The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") in an [*exception-declaration*](#nt:exception-declaration "14.1 Preamble [except.pre]") appertains to the parameter of the catch clause ([[except.handle]](except.handle "14.4 Handling an exception"))[.](#1.sentence-3)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L56)
|
||
|
||
A [*try-block*](#nt:try-block "14.1 Preamble [except.pre]") is a [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]") ([[stmt.pre]](stmt.pre "8.1 Preamble"))[.](#2.sentence-1)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
Within this Clause
|
||
âtry blockâ is taken to mean both [*try-block*](#nt:try-block "14.1 Preamble [except.pre]") and[*function-try-block*](#nt:function-try-block "14.1 Preamble [except.pre]")[.](#2.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L68)
|
||
|
||
The [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of a try block or of a handler is a
|
||
control-flow-limited statement ([[stmt.label]](stmt.label "8.2 Label"))[.](#3.sentence-1)
|
||
|
||
[*Example [1](#example-1)*: void f() {goto l1; // errorgoto l2; // errortry {goto l1; // OKgoto l2; // error l1: ; } catch (...) { l2: ; goto l1; // errorgoto l2; // OK}} â *end example*]
|
||
|
||
Agoto,break,return,
|
||
orcontinue statement can be used to transfer control out of
|
||
a try block or handler[.](#3.sentence-2)
|
||
|
||
When this happens, each variable declared in the try block
|
||
will be destroyed in the context that
|
||
directly contains its declaration[.](#3.sentence-3)
|
||
|
||
[*Example [2](#example-2)*: lab: try { T1 t1; try { T2 t2; if ([*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]"))goto lab; } catch(...) { /* handler 2 */ }} catch(...) { /* handler 1 */ }
|
||
|
||
Here, executinggoto lab; will destroy firstt2,
|
||
thent1,
|
||
assuming the[*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]") does not declare a variable[.](#3.sentence-4)
|
||
|
||
Any exception thrown while destroyingt2 will result in executinghandler 2;
|
||
any exception thrown while destroyingt1 will result in executinghandler 1[.](#3.sentence-5)
|
||
|
||
â *end example*]
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L144)
|
||
|
||
A[*function-try-block*](#nt:function-try-block "14.1 Preamble [except.pre]") associates a[*handler-seq*](#nt:handler-seq "14.1 Preamble [except.pre]") with the[*ctor-initializer*](class.base.init#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]"),
|
||
if present, and the[*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]")[.](#4.sentence-1)
|
||
|
||
An exception
|
||
thrown during the execution of the[*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") or, for constructors and destructors, during the initialization or
|
||
destruction, respectively, of the class's subobjects,
|
||
transfers control to a handler in a[*function-try-block*](#nt:function-try-block "14.1 Preamble [except.pre]") in the same way as an exception thrown during the execution of a[*try-block*](#nt:try-block "14.1 Preamble [except.pre]") transfers control to other handlers[.](#4.sentence-2)
|
||
|
||
[*Example [3](#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) {// constructor statements} catch (...) {// handles exceptions thrown from the ctor-initializer and from the constructor statements} â *end example*]
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L184)
|
||
|
||
In this Clause, âbeforeâ and âafterâ refer to the[âsequenced beforeâ](intro.execution#def:sequenced_before "6.10.1 Sequential execution [intro.execution]") relation[.](#5.sentence-1)
|