Files
2025-10-25 03:02:53 +03:00

100 lines
6.4 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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.1Preamble[except.pre]")
try [*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]") [*handler-seq*](#nt:handler-seq "14.1Preamble[except.pre]")
[function-try-block:](#nt:function-try-block "14.1Preamble[except.pre]")
try [*ctor-initializer*](class.base.init#nt:ctor-initializer "11.9.3Initializing bases and members[class.base.init]")opt [*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]") [*handler-seq*](#nt:handler-seq "14.1Preamble[except.pre]")
[handler-seq:](#nt:handler-seq "14.1Preamble[except.pre]")
[*handler*](#nt:handler "14.1Preamble[except.pre]") [*handler-seq*](#nt:handler-seq "14.1Preamble[except.pre]")opt
[handler:](#nt:handler "14.1Preamble[except.pre]")
catch ( [*exception-declaration*](#nt:exception-declaration "14.1Preamble[except.pre]") ) [*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]")
[exception-declaration:](#nt:exception-declaration "14.1Preamble[except.pre]")
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*type-specifier-seq*](dcl.type.general#nt:type-specifier-seq "9.2.9.1General[dcl.type.general]") [*declarator*](dcl.decl.general#nt:declarator "9.3.1General[dcl.decl.general]")
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*type-specifier-seq*](dcl.type.general#nt:type-specifier-seq "9.2.9.1General[dcl.type.general]") [*abstract-declarator*](dcl.name#nt:abstract-declarator "9.3.2Type names[dcl.name]")opt
...
The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") in an [*exception-declaration*](#nt:exception-declaration "14.1Preamble[except.pre]") appertains to the parameter of the catch clause ([[except.handle]](except.handle "14.4Handling 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.1Preamble[except.pre]") is a [*statement*](stmt.pre#nt:statement "8.1Preamble[stmt.pre]") ([[stmt.pre]](stmt.pre "8.1Preamble"))[.](#2.sentence-1)
[*Note [1](#note-1)*:
Within this Clause
“try block” is taken to mean both [*try-block*](#nt:try-block "14.1Preamble[except.pre]") and[*function-try-block*](#nt:function-try-block "14.1Preamble[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.4Compound statement or block[stmt.block]") of a try block or of a handler is a
control-flow-limited statement ([[stmt.label]](stmt.label "8.2Label"))[.](#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.1Preamble[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.1Preamble[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.1Preamble[except.pre]") associates a[*handler-seq*](#nt:handler-seq "14.1Preamble[except.pre]") with the[*ctor-initializer*](class.base.init#nt:ctor-initializer "11.9.3Initializing bases and members[class.base.init]"),
if present, and the[*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]")[.](#4.sentence-1)
An exception
thrown during the execution of the[*compound-statement*](stmt.block#nt:compound-statement "8.4Compound 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.1Preamble[except.pre]") in the same way as an exception thrown during the execution of a[*try-block*](#nt:try-block "14.1Preamble[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.1Sequential execution[intro.execution]") relation[.](#5.sentence-1)