Skip to content

Commit 6af8a59

Browse files
author
Colin Robertson
committed
Fix 1933 Link to relevant docs + Acrolinx
1 parent 61dd70e commit 6af8a59

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

docs/cpp/try-except-statement.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
---
2-
title: "try-except Statement"
3-
ms.date: "10/09/2018"
2+
title: "try-except statement"
3+
description: "The Microsoft C++ reference to the __try and __except structured exception handling statements."
4+
ms.date: "04/03/2020"
45
f1_keywords: ["_abnormal_termination_cpp", "_exception_code_cpp", "_exception_info", "__except", "_except", "_exception_code", "__except_cpp", "_exception_info_cpp"]
56
helpviewer_keywords: ["__try keyword [C++]", "EXCEPTION_CONTINUE_EXECUTION macro", "EXCEPTION_CONTINUE_SEARCH macro", "EXCEPTION_EXECUTE_HANDLER macro", "GetExceptionCode function", "try-catch keyword [C++], try-except keyword [C++]", "_exception_code keyword [C++]", "try-except keyword [C++]", "_exception_info keyword [C++]", "_abnormal_termination keyword [C++]"]
67
ms.assetid: 30d60071-ea49-4bfb-a8e6-7a420de66381
78
---
8-
# try-except Statement
9+
# try-except statement
910

10-
**Microsoft Specific**
11-
12-
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**.
1312

1413
## Syntax
1514

@@ -24,50 +23,50 @@ The **try-except** statement is a Microsoft extension to the C and C++ languages
2423
2524
## Remarks
2625

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).
2827

2928
For related information, see the [try-finally statement](../cpp/try-finally-statement.md).
3029

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.
3231

3332
> [!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.
3534
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:
3736

3837
1. The guarded section is executed.
3938

4039
1. If no exception occurs during execution of the guarded section, execution continues at the statement after the **__except** clause.
4140

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** expression is evaluated. There are three possible values:
4342

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.
4544

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.
4746

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.
4948

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.
5150

5251
Each application can have its own exception handler.
5352

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.
5554

5655
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.
5756

5857
### The __leave Keyword
5958

6059
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.
6160

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** statement because stack unwinding does not occur. However, we recommend that you use the **__leave** keyword rather than a **goto** statement because 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.
6362

6463
### Structured Exception Handling Intrinsic Functions
6564

66-
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).
6766

6867
`GetExceptionCode` returns the code (a 32-bit integer) of the exception.
6968

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:
7170

7271
```cpp
7372
typedef struct _EXCEPTION_POINTERS {
@@ -78,11 +77,11 @@ typedef struct _EXCEPTION_POINTERS {
7877
7978
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>
8079
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.
8281
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.
8483
85-
excpt.h defines some alternate names for these intrinsics:
84+
\<excpt.h> defines some alternate names for these intrinsics:
8685
8786
`GetExceptionCode` is equivalent to `_exception_code`
8887
@@ -154,8 +153,6 @@ in except
154153
world
155154
```
156155

157-
**END Microsoft Specific**
158-
159156
## See also
160157

161158
[Writing an exception handler](../cpp/writing-an-exception-handler.md)<br/>

0 commit comments

Comments
 (0)