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
The **try-except** statement is a Microsoft extension to the C and C++ languages that supports structured exception handling.
11
+
The **try-except** statement is a Microsoft extension that supports structured exception handling in the C and C++ languages. This extension is **Microsoft-specific**.
13
12
14
13
## Syntax
15
14
@@ -24,50 +23,50 @@ The **try-except** statement is a Microsoft extension to the C and C++ languages
24
23
25
24
## Remarks
26
25
27
-
The **try-except** statement is a Microsoft extension to the C and C++ languages that enables target applications to gain control when events that normally terminate program execution occur. Such events are called *exceptions*, and the mechanism that deals with exceptions is called *structured exception handling* (SEH).
26
+
The **try-except** statement is a Microsoft extension to the C and C++ languages. It enables target applications to gain control when events occur that normally terminate program execution. Such events are called *structured exceptions*, or *exceptions* for short. The mechanism that deals with these exceptions is called *structured exception handling* (SEH).
28
27
29
28
For related information, see the [try-finally statement](../cpp/try-finally-statement.md).
30
29
31
-
Exceptions can be either hardware-based or software-based. Even when applications cannot completely recover from hardware or software exceptions, structured exception handling makes it possible to display error information and trap the internal state of the application to help diagnose the problem. This is especially useful for intermittent problems that cannot be reproduced easily.
30
+
Exceptions may be either hardware-based or software-based. Structured exception handling is useful even when applications can't completely recover from hardware or software exceptions. SEH makes it possible to display error information and trap the internal state of the application to help diagnose the problem. It's especially useful for intermittent problems that aren't easy to reproduce.
32
31
33
32
> [!NOTE]
34
-
> Structured exception handling works with Win32 for both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism ([try, catch, and throw](../cpp/try-throw-and-catch-statements-cpp.md) statements).
33
+
> Structured exception handling works with Win32 for both C and C++ source files. However, it's not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, we recommend you use native C++ exception-handling: [try, catch, and throw](../cpp/try-throw-and-catch-statements-cpp.md) statements.
35
34
36
-
The compound statement after the **__try** clause is the body or guarded section. The compound statement after the **__except** clause is the exception handler. The handler specifies a set of actions to be taken if an exception is raised during execution of the body of the guarded section. Execution proceeds as follows:
35
+
The compound statement after the **__try** clause is the *body* or *guarded* section. The **__except** expression is also known as the *filter* expression. Its value determines how the exception is handled. The compound statement after the **__except** clause is the exception handler. The handler specifies the actions to take if an exception is raised during execution of the body section. Execution proceeds as follows:
37
36
38
37
1. The guarded section is executed.
39
38
40
39
1. If no exception occurs during execution of the guarded section, execution continues at the statement after the **__except** clause.
41
40
42
-
1. If an exception occurs during execution of the guarded section or in any routine the guarded section calls, the **__except***expression* (called the *filter* expression) is evaluated and the value determines how the exception is handled. There are three possible values:
41
+
1. If an exception occurs during execution of the guarded section, or in any routine the guarded section calls, the **__except** expressionis evaluated. There are three possible values:
43
42
44
-
- EXCEPTION_CONTINUE_EXECUTION (-1) Exception is dismissed. Continue execution at the point where the exception occurred.
43
+
-`EXCEPTION_CONTINUE_EXECUTION` (-1) Exception is dismissed. Continue execution at the point where the exception occurred.
45
44
46
-
- EXCEPTION_CONTINUE_SEARCH (0) Exception is not recognized. Continue to search up the stack for a handler, first for containing **try-except** statements, then for handlers with the next highest precedence.
45
+
-`EXCEPTION_CONTINUE_SEARCH` (0) Exception isn't recognized. Continue to search up the stack for a handler, first for containing **try-except** statements, then for handlers with the next highest precedence.
47
46
48
-
- EXCEPTION_EXECUTE_HANDLER (1) Exception is recognized. Transfer control to the exception handler by executing the **__except** compound statement, then continue execution after the **__except** block.
47
+
-`EXCEPTION_EXECUTE_HANDLER` (1) Exception is recognized. Transfer control to the exception handler by executing the **__except** compound statement, then continue execution after the **__except** block.
49
48
50
-
Because the **__except** expression is evaluated as a C expression, it is limited to a single value, the conditional-expression operator, or the comma operator. If more extensive processing is required, the expression can call a routine that returns one of the three values listed above.
49
+
The **__except** expression is evaluated as a C expression. It's limited to a single value, the conditional-expression operator, or the comma operator. If more extensive processing is required, the expression can call a routine that returns one of the three values listed above.
51
50
52
51
Each application can have its own exception handler.
53
52
54
-
It is not valid to jump into a **__try** statement, but valid to jump out of one. The exception handler is not called if a process is terminated in the middle of executing a **try-except** statement.
53
+
It's not valid to jump into a **__try** statement, but valid to jump out of one. The exception handler isn't called if a process is terminated in the middle of executing a **try-except** statement.
55
54
56
55
For compatibility with previous versions, **_try**, **_except**, and **_leave** are synonyms for **__try**, **__except**, and **__leave** unless compiler option [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) is specified.
57
56
58
57
### The __leave Keyword
59
58
60
59
The **__leave** keyword is valid only within the guarded section of a **try-except** statement, and its effect is to jump to the end of the guarded section. Execution continues at the first statement after the exception handler.
61
60
62
-
A **goto** statement can also jump out of the guarded section, and it does not degrade performance as it does in a **try-finally** statementbecause stack unwinding does not occur. However, we recommend that you use the **__leave** keyword rather than a **goto** statementbecause you are less likely to make a programming mistake if the guarded section is large or complex.
61
+
A **goto** statement can also jump out of the guarded section, and it doesn't degrade performance as it does in a **try-finally** statement. That's because stack unwinding doesn't occur. However, we recommend that you use the **__leave** keyword rather than a **goto** statement. The reason is because you're less likely to make a programming mistake if the guarded section is large or complex.
Structured exception handling provides two intrinsic functions that are available to use with the **try-except** statement: `GetExceptionCode` and `GetExceptionInformation`.
65
+
Structured exception handling provides two intrinsic functions that are available to use with the **try-except** statement: [GetExceptionCode](/windows/win32/Debug/getexceptioncode) and [GetExceptionInformation](/windows/win32/Debug/getexceptioninformation).
67
66
68
67
`GetExceptionCode` returns the code (a 32-bit integer) of the exception.
69
68
70
-
The intrinsic function `GetExceptionInformation` returns a pointer to a structure containing additional information about the exception. Through this pointer, you can access the machine state that existed at the time of a hardware exception. The structure is as follows:
69
+
The intrinsic function `GetExceptionInformation` returns a pointer to an [EXCEPTION_POINTERS](/windows/win32/api/winnt/ns-winnt-exception_pointers) structure containing additional information about the exception. Through this pointer, you can access the machine state that existed at the time of a hardware exception. The structure is as follows:
The pointer types `PEXCEPTION_RECORD` and `PCONTEXT` are defined in the include file \<winnt.h>, and `_EXCEPTION_RECORD` and `_CONTEXT` are defined in the include file \<excpt.h>
80
79
81
-
You can use `GetExceptionCode` within the exception handler. However, you can use `GetExceptionInformation` only within the exception filter expression. The information it points to is generally on the stack and is no longer available when control is transferred to the exception handler.
80
+
You can use `GetExceptionCode` within the exception handler. However, you can use `GetExceptionInformation` only within the exception filter expression. The information it points to is generally on the stack and is no longer available when control gets transferred to the exception handler.
82
81
83
-
The intrinsic function `AbnormalTermination` is available within a termination handler. It returns 0 if the body of the **try-finally** statement terminates sequentially. In all other cases, it returns 1.
82
+
The intrinsic function [AbnormalTermination](/windows/win32/Debug/abnormaltermination) is available within a termination handler. It returns 0 if the body of the **try-finally** statement terminates sequentially. In all other cases, it returns 1.
84
83
85
-
excpt.h defines some alternate names for these intrinsics:
84
+
\<excpt.h> defines some alternate names for these intrinsics:
86
85
87
86
`GetExceptionCode` is equivalent to `_exception_code`
88
87
@@ -154,8 +153,6 @@ in except
154
153
world
155
154
```
156
155
157
-
**END Microsoft Specific**
158
-
159
156
## See also
160
157
161
158
[Writing an exception handler](../cpp/writing-an-exception-handler.md)<br/>
0 commit comments