Skip to content

Commit 0b4cf27

Browse files
Improve code examples in writing-an-exception-filter.md (#5081)
* Update writing-an-exception-filter.md * Another correction
1 parent 6b01e57 commit 0b4cf27

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

docs/cpp/writing-an-exception-filter.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,37 @@ For example, the following code uses a function call in the *filter* expression:
1616
```cpp
1717
// exceptions_Writing_an_Exception_Filter.cpp
1818
#include <windows.h>
19+
int Eval_Exception(int);
1920
int main() {
20-
int Eval_Exception( int );
21-
22-
__try {}
23-
24-
__except ( Eval_Exception( GetExceptionCode( ))) {
25-
;
26-
}
27-
21+
__try {
22+
;
23+
}
24+
__except (Eval_Exception(GetExceptionCode())) {
25+
;
26+
}
27+
}
28+
void HandleOverflow() {
29+
// Gracefully recover
2830
}
29-
void ResetVars( int ) {}
30-
int Eval_Exception ( int n_except ) {
31-
if ( n_except != STATUS_INTEGER_OVERFLOW &&
32-
n_except != STATUS_FLOAT_OVERFLOW ) // Pass on most exceptions
33-
return EXCEPTION_CONTINUE_SEARCH;
34-
35-
// Execute some code to clean up problem
36-
ResetVars( 0 ); // initializes data to 0
37-
return EXCEPTION_CONTINUE_EXECUTION;
31+
int Eval_Exception(int n_except) {
32+
if (
33+
n_except != STATUS_INTEGER_OVERFLOW &&
34+
n_except != STATUS_FLOAT_OVERFLOW
35+
) {
36+
// Pass on most exceptions
37+
return EXCEPTION_CONTINUE_SEARCH;
38+
}
39+
// Execute some code to clean up problem
40+
HandleOverflow();
41+
return EXCEPTION_CONTINUE_EXECUTION;
3842
}
3943
```
4044
4145
It's a good idea to use a function call in the *filter* expression whenever *filter* needs to do anything complex. Evaluating the expression causes execution of the function, in this case, `Eval_Exception`.
4246
4347
Note the use of [`GetExceptionCode`](/windows/win32/Debug/getexceptioncode) to determine the exception. This function must be called inside the filter expression of the **`__except`** statement. `Eval_Exception` can't call `GetExceptionCode`, but it must have the exception code passed to it.
4448
45-
This handler passes control to another handler unless the exception is an integer or floating-point overflow. If it is, the handler calls a function (`ResetVars` is only an example, not an API function) to reset some global variables. The **`__except`** statement block, which in this example is empty, can never be executed because `Eval_Exception` never returns `EXCEPTION_EXECUTE_HANDLER` (1).
49+
This handler passes control to another handler unless the exception is an integer or floating-point overflow. If it is, the handler calls a function (`HandleOverflow` is only an example, not an API function) to appropriately try to recover from the exception. The **`__except`** statement block, which in this example is empty, can never be executed because `Eval_Exception` never returns `EXCEPTION_EXECUTE_HANDLER` (1).
4650
4751
Using a function call is a good general-purpose technique for dealing with complex filter expressions. Two other C language features that are useful are:
4852
@@ -53,21 +57,21 @@ Using a function call is a good general-purpose technique for dealing with compl
5357
The conditional operator is frequently useful here. It can be used to check for a specific return code and then return one of two different values. For example, the filter in the following code recognizes the exception only if the exception is `STATUS_INTEGER_OVERFLOW`:
5458
5559
```cpp
56-
__except( GetExceptionCode() == STATUS_INTEGER_OVERFLOW ? 1 : 0 ) {
60+
__except (GetExceptionCode() == STATUS_INTEGER_OVERFLOW ? 1 : 0)
5761
```
5862

5963
The purpose of the conditional operator in this case is mainly to provide clarity, because the following code produces the same results:
6064

6165
```cpp
62-
__except( GetExceptionCode() == STATUS_INTEGER_OVERFLOW ) {
66+
__except (GetExceptionCode() == STATUS_INTEGER_OVERFLOW)
6367
```
6468
6569
The conditional operator is more useful in situations where you might want the filter to evaluate to -1, `EXCEPTION_CONTINUE_EXECUTION`.
6670
6771
The comma operator lets you execute multiple expressions in sequence. It then returns the value of the last expression. For example, the following code stores the exception code in a variable and then tests it:
6872
6973
```cpp
70-
__except( nCode = GetExceptionCode(), nCode == STATUS_INTEGER_OVERFLOW )
74+
__except (nCode = GetExceptionCode(), nCode == STATUS_INTEGER_OVERFLOW)
7175
```
7276

7377
## See also

0 commit comments

Comments
 (0)