Skip to content

Commit e9998aa

Browse files
author
Colin Robertson
committed
Fix __leave, __try keywords; cpp-docs 2413
1 parent 3ad4780 commit e9998aa

16 files changed

Lines changed: 280 additions & 252 deletions

docs/build/reference/eh-exception-handling-model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ When you use **`/EHs`** or **`/EHsc`**, the compiler assumes that exceptions can
5353

5454
### Structured and standard C++ exception handling
5555

56-
The **`/EHa`** compiler option enables safe stack unwinding for both asynchronous exceptions and C++ exceptions. It supports handling of both standard C++ and structured exceptions by using the native C++ `catch(...)` clause. To implement SEH without specifying **`/EHa`**, you may use the **__try**, **`__except`**, and **`__finally`** syntax. For more information, see [Structured exception handling](../../cpp/structured-exception-handling-c-cpp.md).
56+
The **`/EHa`** compiler option enables safe stack unwinding for both asynchronous exceptions and C++ exceptions. It supports handling of both standard C++ and structured exceptions by using the native C++ `catch(...)` clause. To implement SEH without specifying **`/EHa`**, you may use the **`__try`**, **`__except`**, and **`__finally`** syntax. For more information, see [Structured exception handling](../../cpp/structured-exception-handling-c-cpp.md).
5757

5858
> [!IMPORTANT]
5959
> Specifying **`/EHa`** and trying to handle all exceptions by using `catch(...)` can be dangerous. In most cases, asynchronous exceptions are unrecoverable and should be considered fatal. Catching them and proceeding can cause process corruption and lead to bugs that are hard to find and fix.
Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
11
---
2-
title: "Summary of Statements"
3-
ms.date: "11/04/2016"
2+
title: "Summary of C statements"
3+
description: "A summary of the statement grammar in the Microsoft C implementation."
4+
ms.date: 08/24/2020
45
ms.assetid: ce45d2fe-ec0e-459f-afb1-80ab6a7f0239
56
---
6-
# Summary of Statements
7-
8-
*statement*:<br/>
9-
&nbsp;&nbsp;&nbsp;&nbsp;*labeled-statement*<br/>
10-
&nbsp;&nbsp;&nbsp;&nbsp;*compound-statement*<br/>
11-
&nbsp;&nbsp;&nbsp;&nbsp;*expression-statement*<br/>
12-
&nbsp;&nbsp;&nbsp;&nbsp;*selection-statement*<br/>
13-
&nbsp;&nbsp;&nbsp;&nbsp;*iteration-statement*<br/>
14-
&nbsp;&nbsp;&nbsp;&nbsp;*jump-statement*<br/>
15-
&nbsp;&nbsp;&nbsp;&nbsp;*try-except-statement* /\* Microsoft-specific \*/<br/>
16-
&nbsp;&nbsp;&nbsp;&nbsp;*try-finally-statement* /\* Microsoft-specific \*/
17-
18-
*jump-statement*:<br/>
19-
&nbsp;&nbsp;&nbsp;&nbsp;**`goto`** *identifier* **;**<br/>
20-
&nbsp;&nbsp;&nbsp;&nbsp;**continue ;**<br/>
21-
&nbsp;&nbsp;&nbsp;&nbsp;**break ;**<br/>
22-
&nbsp;&nbsp;&nbsp;&nbsp;**`return`** *expression*<sub>opt</sub> **;**
23-
24-
*compound-statement*:<br/>
25-
&nbsp;&nbsp;&nbsp;&nbsp;**{** *declaration-list*<sub>opt</sub> *statement-list*<sub>opt</sub> **}**
26-
27-
*declaration-list*:<br/>
28-
&nbsp;&nbsp;&nbsp;&nbsp;*declaration*<br/>
29-
&nbsp;&nbsp;&nbsp;&nbsp;*declaration-list* *declaration*
30-
31-
*statement-list*:<br/>
32-
&nbsp;&nbsp;&nbsp;&nbsp;*statement*<br/>
33-
&nbsp;&nbsp;&nbsp;&nbsp;*statement-list* *statement*
34-
35-
*expression-statement*:<br/>
36-
&nbsp;&nbsp;&nbsp;&nbsp;*expression*<sub>opt</sub> **;**
37-
38-
*iteration-statement*:<br/>
39-
&nbsp;&nbsp;&nbsp;&nbsp;**while (** *expression* **)** *statement*<br/>
40-
&nbsp;&nbsp;&nbsp;&nbsp;**`do`** *statement* **while (** *expression* **) ;**<br/>
41-
&nbsp;&nbsp;&nbsp;&nbsp;**for (** *expression*<sub>opt</sub> **;** *expression*<sub>opt</sub> **;** *expression*<sub>opt</sub> **)** *statement*
42-
43-
*selection-statement*:<br/>
44-
&nbsp;&nbsp;&nbsp;&nbsp;**if (** *expression* **)** *statement*<br/>
45-
&nbsp;&nbsp;&nbsp;&nbsp;**if (** *expression* **)** *statement* **`else`** *statement*<br/>
46-
&nbsp;&nbsp;&nbsp;&nbsp;**switch (** *expression* **)** *statement*
47-
48-
*labeled-statement*:<br/>
49-
&nbsp;&nbsp;&nbsp;&nbsp;*identifier* **:** *statement*<br/>
50-
&nbsp;&nbsp;&nbsp;&nbsp;**`case`** *constant-expression* **:** *statement*<br/>
51-
&nbsp;&nbsp;&nbsp;&nbsp;**default :** *statement*
52-
53-
*try-except-statement*: /\* Microsoft-specific \*/<br/>
54-
&nbsp;&nbsp;&nbsp;&nbsp;**__try** *compound-statement* **__except (** *expression* **)** *compound-statement*
55-
56-
*try-finally-statement*: /\* Microsoft-specific \*/<br/>
57-
&nbsp;&nbsp;&nbsp;&nbsp;**__try** *compound-statement* **`__finally`** *compound-statement*
7+
# Summary of C statements
8+
9+
*`statement`*:<br/>
10+
&emsp;*`labeled-statement`*<br/>
11+
&emsp;*`compound-statement`*<br/>
12+
&emsp;*`expression-statement`*<br/>
13+
&emsp;*`selection-statement`*<br/>
14+
&emsp;*`iteration-statement`*<br/>
15+
&emsp;*`jump-statement`*<br/>
16+
&emsp;*`try-except-statement`* /\* Microsoft-specific \*/<br/>
17+
&emsp;*`try-finally-statement`* /\* Microsoft-specific \*/
18+
19+
*`jump-statement`*:<br/>
20+
&emsp;**`goto`** *`identifier`* **`;`**<br/>
21+
&emsp;**`continue ;`**<br/>
22+
&emsp;**`break ;`**<br/>
23+
&emsp;**`return`** *`expression`*<sub>opt</sub> **`;`**<br/>
24+
&emsp;**`__leave ;`** /\* Microsoft-specific<sup>1</sup> \*/
25+
26+
*`compound-statement`*:<br/>
27+
&emsp;**`{`** *`declaration-list`*<sub>opt</sub> *`statement-list`*<sub>opt</sub> **`}`**
28+
29+
*`declaration-list`*:<br/>
30+
&emsp;*`declaration`*<br/>
31+
&emsp;*`declaration-list`* *`declaration`*
32+
33+
*`statement-list`*:<br/>
34+
&emsp;*`statement`*<br/>
35+
&emsp;*`statement-list`* *`statement`*
36+
37+
*`expression-statement`*:<br/>
38+
&emsp;*`expression`*<sub>opt</sub> **`;`**
39+
40+
*`iteration-statement`*:<br/>
41+
&emsp;**`while (`** *`expression`* **`)`** *`statement`*<br/>
42+
&emsp;**`do`** *`statement`* **`while (`** *`expression`* **`) ;`**<br/>
43+
&emsp;**`for (`** *`expression`*<sub>opt</sub> **`;`** *`expression`*<sub>opt</sub> **`;`** *`expression`*<sub>opt</sub> **`)`** *`statement`*
44+
45+
*`selection-statement`*:<br/>
46+
&emsp;**`if (`** *`expression`* **`)`** *`statement`*<br/>
47+
&emsp;**`if (`** *`expression`* **`)`** *`statement`* **`else`** *`statement`*<br/>
48+
&emsp;**`switch (`** *`expression`* **`)`** *`statement`*
49+
50+
*`labeled-statement`*:<br/>
51+
&emsp;*`identifier`* **`:`** *`statement`*<br/>
52+
&emsp;**`case`** *`constant-expression`* **`:`** *`statement`*<br/>
53+
&emsp;**`default :`** *`statement`*
54+
55+
*`try-except-statement`*: /\* Microsoft-specific \*/<br/>
56+
&emsp;**`__try`** *`compound-statement`* **`__except (`** *`expression`* **`)`** *`compound-statement`*
57+
58+
*`try-finally-statement`*: /\* Microsoft-specific \*/<br/>
59+
&emsp;**`__try`** *`compound-statement`* **`__finally`** *`compound-statement`*
60+
61+
1 The **`__leave`** keyword is only valid within the **`__try`** block of a *`try-except-statement`* or a *`try-finally-statement`*.
5862

5963
## See also
6064

61-
[Phrase Structure Grammar](../c-language/phrase-structure-grammar.md)
65+
[Phrase structure grammar](../c-language/phrase-structure-grammar.md)
Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,85 @@
11
---
2-
title: "try-except Statement (C)"
3-
ms.date: "11/04/2016"
2+
title: "try-except statement (C)"
3+
description: "Microsoft C/C++ implements Structured Exception Handling (SEH) using a try-except statement language extension."
4+
ms.date: 08/24/2020
45
helpviewer_keywords: ["try-except keyword [C]", "structured exception handling, try-except", "try-catch keyword [C]", "__try keyword [C]", "__except keyword [C]", "__except keyword [C], in try-except", "try-catch keyword [C], try-except keyword [C]"]
56
ms.assetid: f76db9d1-fc78-417f-b71f-18e545fc01c3
67
---
7-
# try-except Statement (C)
8+
# try-except statement (C)
89

9-
**Microsoft Specific**
10+
**Microsoft-specific**
1011

11-
The **try-except** statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.
12+
The `try-except` statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.
1213

13-
Exceptions can be either hardware- 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.
14+
Exceptions may be either hardware- or software-based. Even when applications can't completely recover from hardware or software exceptions, structured exception handling makes it possible to log and display error information. It's useful to trap the internal state of the application to help diagnose the problem. In particular, it's helpful for intermittent problems that aren't easy to reproduce.
1415

1516
## Syntax
1617

17-
*try-except-statement*:
18-
**__try** *compound-statement*
18+
> *`try-except-statement`*:\
19+
> &emsp;**`__try`** *`compound-statement`* **`__except (`** *`expression`* **`)`** *`compound-statement`*
1920
20-
**__except (** *expression* **)** *compound-statement*
21-
22-
The compound statement after the `__try` clause is the 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 guarded section. Execution proceeds as follows:
21+
The compound statement after the **`__try`** clause is the *guarded section*. The compound statement after the **`__except`** clause is the *exception handler*. The handler specifies a set of actions to take if an exception is raised during execution of the guarded section. Execution proceeds as follows:
2322

2423
1. The guarded section is executed.
2524

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

28-
1. If an exception occurs during execution of the guarded section or in any routine the guarded section calls, the **`__except`** expression is evaluated and the value returned determines how the exception is handled. There are three values:
29-
30-
`EXCEPTION_CONTINUE_SEARCH` 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.
27+
1. If an exception occurs during execution of the guarded section, or in any routine the guarded section calls, the **`__except`** expression gets evaluated. The value returned determines how the exception is handled. There are three possible values:
3128

32-
`EXCEPTION_CONTINUE_EXECUTION` Exception is recognized but dismissed. Continue execution at the point where the exception occurred.
29+
- `EXCEPTION_CONTINUE_SEARCH`: The 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.
3330

34-
`EXCEPTION_EXECUTE_HANDLER` Exception is recognized. Transfer control to the exception handler by executing the **`__except`** compound statement, then continue execution at the point the exception occurred.
31+
- `EXCEPTION_CONTINUE_EXECUTION`: The exception is recognized but dismissed. Continue execution at the point where the exception occurred.
3532

36-
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.
33+
- `EXCEPTION_EXECUTE_HANDLER` The exception is recognized. Transfer control to the exception handler by executing the **`__except`** compound statement, then continue execution at the point the exception occurred.
3734

38-
> [!NOTE]
39-
> Structured exception handling works with 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, the C++ exception handling mechanism is much more flexible, in that it can handle exceptions of any type.
35+
Because the **`__except`** expression is evaluated as a C expression, it's limited to either 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.
4036

4137
> [!NOTE]
42-
> For C++ programs, C++ exception handling should be used instead of structured exception handling. For more information, see [Exception Handling](../cpp/exception-handling-in-visual-cpp.md) in the *C++ Language Reference*.
38+
> Structured exception handling works with C and C++ source files. However, it isn't specifically designed for C++. For portable C++ programs, C++ exception handling should be used instead of structured exception handling. Also, the C++ exception handling mechanism is much more flexible, in that it can handle exceptions of any type. For more information, see [Exception handling](../cpp/exception-handling-in-visual-cpp.md) in the *C++ Language Reference*.
4339
44-
Each routine in an application can have its own exception handler. The **`__except`** expression executes in the scope of the `__try` body. This means it has access to any local variables declared there.
40+
Each routine in an application can have its own exception handler. The **`__except`** expression executes in the scope of the **`__try`** body. It has access to any local variables declared there.
4541

46-
The **`__leave** keyword is valid within a **try-except** statement block. The effect of **`__leave** is to jump to the end of the **try-except** block. Execution resumes after the end of the exception handler. Although a **`goto`** statement can be used to accomplish the same result, a **`goto`** statement causes stack unwinding. The **`__leave** statement is more efficient because it does not involve stack unwinding.
42+
The **`__leave`** keyword is valid within a `try-except` statement block. The effect of **`__leave`** is to jump to the end of the `try-except` block. Execution resumes after the end of the exception handler. Although a **`goto`** statement can be used to accomplish the same result, a **`goto`** statement causes stack unwinding. The **`__leave`** statement is more efficient because it doesn't involve stack unwinding.
4743

48-
Exiting a **try-except** statement using the `longjmp` run-time function is considered abnormal termination. It is illegal to jump into a `__try` statement, but legal to jump out of one. The exception handler is not called if a process is killed in the middle of executing a **try-except** statement.
44+
Exiting a `try-except` statement using the `longjmp` run-time function is considered abnormal termination. It isn't legal to jump into a **`__try`** statement, but it's legal to jump out of one. The exception handler isn't called if a process is killed in the middle of executing a `try-except` statement.
4945

5046
## Example
5147

52-
Following is an example of an exception handler and a termination handler. See [The try-finally Statement](../c-language/try-finally-statement-c.md) for more information about termination handlers.
48+
Here's an example of an exception handler and a termination handler. For more information about termination handlers, see [`try-finally` statement (C)](../c-language/try-finally-statement-c.md).
5349

54-
```
50+
```C
5551
.
5652
.
5753
.
5854
puts("hello");
59-
__try{
55+
__try {
6056
puts("in try");
61-
__try{
57+
__try {
6258
puts("in try");
6359
RAISE_AN_EXCEPTION();
64-
}__finally{
60+
} __finally {
6561
puts("in finally");
6662
}
67-
}__except( puts("in filter"), EXCEPTION_EXECUTE_HANDLER ){
63+
} __except( puts("in filter"), EXCEPTION_EXECUTE_HANDLER ) {
6864
puts("in except");
6965
}
7066
puts("world");
7167
```
7268
73-
This is the output from the example, with commentary added on the right:
69+
Here's the output from the example, with commentary added on the right:
7470
75-
```
71+
```Output
7672
hello
77-
in try /* fall into try */
78-
in try /* fall into nested try */
73+
in try /* fall into try */
74+
in try /* fall into nested try */
7975
in filter /* execute filter; returns 1 so accept */
8076
in finally /* unwind nested finally */
8177
in except /* transfer control to selected handler */
8278
world /* flow out of handler */
8379
```
8480

85-
**END Microsoft Specific**
81+
**END Microsoft-specific**
8682

8783
## See also
8884

89-
[try-except Statement](../cpp/try-except-statement.md)
85+
[`try-except` statement (C++)](../cpp/try-except-statement.md)

0 commit comments

Comments
 (0)