You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exception handling provides a way of transferring control and information from some point in the execution of a program to a handler associated with a point previously passed by the execution (in other words, exception handling transfers control up the call stack).
15
15
16
-
Evaluating a <DocLinksrc="cpp/language/throw.html#throw_expressions">`throw` expression</DocLink> will throw an exception. Exceptions can also be thrown in <DocLinksrc="cpp/language/throw.html#throw_expressions">other contexts</DocLink>.
16
+
Evaluating a <DocLinkdest="cpp/language/throw.html#throw_expressions">`throw` expression</DocLink> will throw an exception. Exceptions can also be thrown in <DocLinkdest="cpp/language/throw.html#throw_expressions">other contexts</DocLink>.
17
17
18
-
In order for an exception to be caught, the `throw` expression has to be inside a <DocLinksrc="cpp/language/try">`try` block</DocLink>, and the `try` block has to contain a <DocLinksrc="cpp/language/catch">handler</DocLink> that matches the type of the exception object.
18
+
In order for an exception to be caught, the `throw` expression has to be inside a <DocLinkdest="cpp/language/try">`try` block</DocLink>, and the `try` block has to contain a <DocLinkdest="cpp/language/catch">handler</DocLink> that matches the type of the exception object.
19
19
20
20
When declaring a function, the following specification(s) may be provided to limit the types of the exceptions a function may throw:
Errors that arise during exception handling are handled by <DocLinksrc="cpp/error/terminate">`std::terminate`</DocLink> and <Revisionsince="C++17"><DocLinksrc="cpp/error/unexpected">`std::unexpected`</DocLink></Revision>.
25
+
Errors that arise during exception handling are handled by <DocLinkdest="cpp/error/terminate">`std::terminate`</DocLink> and <Revisionsince="C++17"><DocLinkdest="cpp/error/unexpected">`std::unexpected`</DocLink></Revision>.
26
26
27
27
## Usage
28
28
29
-
While `throw` expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to <DocLinksrc="cpp/utility/program/longjmp">`std::longjmp`</DocLink>), its intended usage is error handling.
29
+
While `throw` expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to <DocLinkdest="cpp/utility/program/longjmp">`std::longjmp`</DocLink>), its intended usage is error handling.
30
30
31
31
### Error handling
32
32
@@ -36,24 +36,24 @@ Throwing an exception is used to signal errors from functions, where "errors" ar
36
36
2. Failures to meet the preconditions of another function that must be called.
37
37
3. (for non-private member functions) Failures to (re)establish a class invariant.
38
38
39
-
In particular, this implies that the failures of constructors (see also <DocLinksrc="cpp/language/raii">RAII</DocLink>) and most operators should be reported by throwing exceptions.
39
+
In particular, this implies that the failures of constructors (see also <DocLinkdest="cpp/language/raii">RAII</DocLink>) and most operators should be reported by throwing exceptions.
40
40
41
-
In addition, so-called *wide contract* functions use exceptions to indicate unacceptable inputs, for example, <DocLinksrc="cpp/string/basic_string/at">`std::basic_string::at`</DocLink> has no preconditions, but throws an exception to indicate index out of range.
41
+
In addition, so-called *wide contract* functions use exceptions to indicate unacceptable inputs, for example, <DocLinkdest="cpp/string/basic_string/at">`std::basic_string::at`</DocLink> has no preconditions, but throws an exception to indicate index out of range.
42
42
43
43
### Exception safety
44
44
45
45
After the error condition is reported by a function, additional guarantees may be provided with regards to the state of the program. The following four levels of exception guarantee are generally recognized[^4][^5][^6], which are strict supersets of each other:
46
46
47
-
1.*Nothrow (or nofail) exception guarantee* — the function never throws exceptions. Nothrow (errors are reported by other means or concealed) is expected of <DocLinksrc="cpp/language/destructor">destructors</DocLink> and other functions that may be called during stack unwinding. <Revisionsince="C++11">The <DocLinksrc="cpp/language/destructor">destructors</DocLink> are <DocLinksrc="cpp/language/noexcept">`noexcept`</DocLink> by default.</Revision> Nofail (the function always succeeds) is expected of swaps, <DocLinksrc="cpp/language/move_constructor">move constructors</DocLink>, and other functions used by those that provide strong exception guarantee.
48
-
2.*Strong exception guarantee* — If the function throws an exception, the state of the program is rolled back to the state just before the function call (for example, <DocLinksrc="cpp/container/vector/push_back">`std::vector::push_back`</DocLink>).
47
+
1.*Nothrow (or nofail) exception guarantee* — the function never throws exceptions. Nothrow (errors are reported by other means or concealed) is expected of <DocLinkdest="cpp/language/destructor">destructors</DocLink> and other functions that may be called during stack unwinding. <Revisionsince="C++11">The <DocLinkdest="cpp/language/destructor">destructors</DocLink> are <DocLinkdest="cpp/language/noexcept">`noexcept`</DocLink> by default.</Revision> Nofail (the function always succeeds) is expected of swaps, <DocLinkdest="cpp/language/move_constructor">move constructors</DocLink>, and other functions used by those that provide strong exception guarantee.
48
+
2.*Strong exception guarantee* — If the function throws an exception, the state of the program is rolled back to the state just before the function call (for example, <DocLinkdest="cpp/container/vector/push_back">`std::vector::push_back`</DocLink>).
49
49
3.*Basic exception guarantee* — If the function throws an exception, the program is in a valid state. No resources are leaked, and all objects' invariants are intact.
50
50
4.*No exception guarantee* — If the function throws an exception, the program may not be in a valid state: resource leaks, memory corruption, or other invariant-destroying errors may have occurred.
51
51
52
-
Generic components may, in addition, offer *exception-neutral guarantee*: if an exception is thrown from a template parameter (e.g. from the `Compare` function object of <DocLinksrc="cpp/algorithm/sort">`std::sort`</DocLink> or from the constructor of `T` in <DocLinksrc="cpp/memory/shared_ptr/make_shared">`std::make_shared`</DocLink>), it is propagated, unchanged, to the caller.
52
+
Generic components may, in addition, offer *exception-neutral guarantee*: if an exception is thrown from a template parameter (e.g. from the `Compare` function object of <DocLinkdest="cpp/algorithm/sort">`std::sort`</DocLink> or from the constructor of `T` in <DocLinkdest="cpp/memory/shared_ptr/make_shared">`std::make_shared`</DocLink>), it is propagated, unchanged, to the caller.
53
53
54
54
## Exception objects
55
55
56
-
While objects of any complete type and cv pointers to `void` may be thrown as exception objects, all standard library functions throw unnamed objects by value, and the types of those objects are derived (directly or indirectly) from <DocLinksrc="cpp/error/exception">`std::exception`</DocLink>. User-defined exceptions usually follow this pattern.[^7][^8][^9]
56
+
While objects of any complete type and cv pointers to `void` may be thrown as exception objects, all standard library functions throw unnamed objects by value, and the types of those objects are derived (directly or indirectly) from <DocLinkdest="cpp/error/exception">`std::exception`</DocLink>. User-defined exceptions usually follow this pattern.[^7][^8][^9]
57
57
58
58
To avoid unnecessary copying of the exception object and object slicing, the best practice for handlers is to catch by reference.[^10][^11][^12][^13]
The preprocessor is executed at <DocLinksrc="/cpp/language/translation_phases"anchor="Phase_4">translation phase 4</DocLink>, before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler.
11
+
The preprocessor is executed at <DocLinkdest="/cpp/language/translation_phases"anchor="Phase_4">translation phase 4</DocLink>, before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler.
12
12
13
13
## Directives
14
14
@@ -17,11 +17,11 @@ The preprocessing directives control the behavior of the preprocessor. Each dire
17
17
* the `#` character.
18
18
* a sequence of:
19
19
* a standard-defined directive name (listed [below](#Capabilities)) followed by the corresponding arguments, or
20
-
* one or more <DocLinksrc="/cpp/language/translation_phases"anchor="Phase_3">preprocessing tokens</DocLink> where the beginning token is not a standard-defined directive name, in this case the directive is conditionally-supported with implementation-defined semantics <Revisionuntil="C++23">(e.g. a common extension is the directive `#warning` which emits a user-defined message during compilation)</Revision>, or
20
+
* one or more <DocLinkdest="/cpp/language/translation_phases"anchor="Phase_3">preprocessing tokens</DocLink> where the beginning token is not a standard-defined directive name, in this case the directive is conditionally-supported with implementation-defined semantics <Revisionuntil="C++23">(e.g. a common extension is the directive `#warning` which emits a user-defined message during compilation)</Revision>, or
21
21
* nothing, in this case the directive has no effect.
22
22
* a line break.
23
23
24
-
<Revisionsince="C++20">The <DocLinksrc="/cpp/language/modules">module and import directives</DocLink> are also preprocessing directives.</Revision>
24
+
<Revisionsince="C++20">The <DocLinkdest="/cpp/language/modules">module and import directives</DocLink> are also preprocessing directives.</Revision>
25
25
26
26
Preprocessing directives must not come from macro expansion.
27
27
@@ -34,15 +34,15 @@ EMPTY # include <file.h> // not a preprocessing directive
34
34
35
35
The preprocessor has the source file translation capabilities:
36
36
37
-
***<DocLinksrc="/cpp/preprocessor/conditional">conditionally</DocLink>** compile parts of source file (controlled by directive `#if`, `#ifdef`, `#ifndef`, `#else`, <Revisionsince="C++23">`#elif`, `#elifdef`, `#elifndef`</Revision>, and `#endif`).
38
-
***<DocLinksrc="/cpp/preprocessor/replace">replace</DocLink>** text macros while possibly concatenating or quoting identifiers (controlled by directives `#define` and `#undef`, and operators `#` and `##`).
39
-
***<DocLinksrc="/cpp/preprocessor/include">include</DocLink>** other files (controlled by directive `#include` <Revisionsince="C++23">and checked with `__has_include` </Revision>).
40
-
* cause an **<DocLinksrc="/cpp/preprocessor/error">error</DocLink>** <Revisionsince="C++17">or **<DocLinksrc="/cpp/preprocessor/error">warning</DocLink>** </Revision> (controlled by directive `#error` <Revisionsince="C++23">or `#warning` respectively</Revision>).
37
+
***<DocLinkdest="/cpp/preprocessor/conditional">conditionally</DocLink>** compile parts of source file (controlled by directive `#if`, `#ifdef`, `#ifndef`, `#else`, <Revisionsince="C++23">`#elif`, `#elifdef`, `#elifndef`</Revision>, and `#endif`).
38
+
***<DocLinkdest="/cpp/preprocessor/replace">replace</DocLink>** text macros while possibly concatenating or quoting identifiers (controlled by directives `#define` and `#undef`, and operators `#` and `##`).
39
+
***<DocLinkdest="/cpp/preprocessor/include">include</DocLink>** other files (controlled by directive `#include` <Revisionsince="C++23">and checked with `__has_include` </Revision>).
40
+
* cause an **<DocLinkdest="/cpp/preprocessor/error">error</DocLink>** <Revisionsince="C++17">or **<DocLinkdest="/cpp/preprocessor/error">warning</DocLink>** </Revision> (controlled by directive `#error` <Revisionsince="C++23">or `#warning` respectively</Revision>).
41
41
42
42
The following aspects of the preprocessor can be controlled:
43
43
44
-
***<DocLinksrc="/cpp/preprocessor/impl">implementation-defined</DocLink>** behavior (controlled by directive `#pragma` <Revisionsince="C++11">and operator `_Pragma` </Revision>). In addition, some compilers support (to varying degrees) the operator `__pragma` as an extension.
45
-
***<DocLinksrc="/cpp/preprocessor/line">file name and line information</DocLink>** available to the preprocessor (controlled by directive `#line`).
44
+
***<DocLinkdest="/cpp/preprocessor/impl">implementation-defined</DocLink>** behavior (controlled by directive `#pragma` <Revisionsince="C++11">and operator `_Pragma` </Revision>). In addition, some compilers support (to varying degrees) the operator `__pragma` as an extension.
45
+
***<DocLinkdest="/cpp/preprocessor/line">file name and line information</DocLink>** available to the preprocessor (controlled by directive `#line`).
46
46
47
47
## Defect reports
48
48
@@ -63,12 +63,12 @@ The following behavior-changing defect reports were applied retroactively to pre
63
63
64
64
<DescList>
65
65
<Desc>
66
-
<DocLinkslot="item"src="/cpp/preprocessor/replace"anchor="Predefined_macros">C++ documentation</DocLink> for <span>Predefined Macro Symbols</span>
66
+
<DocLinkslot="item"dest="/cpp/preprocessor/replace"anchor="Predefined_macros">C++ documentation</DocLink> for <span>Predefined Macro Symbols</span>
67
67
</Desc>
68
68
<Desc>
69
-
<DocLinkslot="item"src="/cpp/symbol_index/macro">C++ documentation</DocLink> for <span>Macro Symbol Index</span>
69
+
<DocLinkslot="item"dest="/cpp/symbol_index/macro">C++ documentation</DocLink> for <span>Macro Symbol Index</span>
70
70
</Desc>
71
71
<Desc>
72
-
<DocLinkslot="item"src="/c/preprocessor">C documentation</DocLink> for <span>preprocessor</span>
72
+
<DocLinkslot="item"dest="/c/preprocessor">C documentation</DocLink> for <span>preprocessor</span>
0 commit comments