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
Copy file name to clipboardExpand all lines: docs/cpp/exception-handling-differences.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
---
2
2
title: "Handle structured exceptions in C++"
3
-
ms.date: "08/14/2018"
3
+
description: How to handle structured exceptions using the C++ exception handling model.
4
+
ms.date: "09/19/2019"
4
5
helpviewer_keywords: ["structured exception handling [C++], vs. C++ exception handling", "structured exception handling [C++], vs. unstructured", "exceptions [C++], wrapper class", "C++ exception handling [C++], vs. structured exception handling", "wrapper classes [C++], C exception"]
5
6
ms.assetid: f21d1944-4810-468e-b02a-9f77da4138c9
6
7
---
@@ -10,9 +11,9 @@ The major difference between C structured exception handling (SEH) and C++ excep
10
11
11
12
A second difference is that the C structured exception handling model is referred to as *asynchronous*, because exceptions occur secondary to the normal flow of control. The C++ exception handling mechanism is fully *synchronous*, which means that exceptions occur only when they are thrown.
12
13
13
-
When you use the [/EHs or /EHsc](../build/reference/eh-exception-handling-model.md) compiler option, no C++ exception handlers handle structured exceptions. These exceptions are handled only by **__catch** structured exception handlers or **__finally** structured termination handlers. For information, see [Structured Exception Handling (C/C++)](structured-exception-handling-c-cpp.md).
14
+
When you use the [/EHs or /EHsc](../build/reference/eh-exception-handling-model.md) compiler option, no C++ exception handlers handle structured exceptions. These exceptions are handled only by **__except** structured exception handlers or **__finally** structured termination handlers. For information, see [Structured Exception Handling (C/C++)](structured-exception-handling-c-cpp.md).
14
15
15
-
Under the [/EHa](../build/reference/eh-exception-handling-model.md) compiler option, if a C exception is raised in a C++ program, it can be handled by a structured exception handler with its associated filter or by a C++ **catch** handler, whichever is dynamically nearer to the exception context. For example, the following C++ program raises a C exception inside a C++ **try** context:
16
+
Under the [/EHa](../build/reference/eh-exception-handling-model.md) compiler option, if a C exception is raised in a C++ program, it can be handled by a structured exception handler with its associated filter or by a C++ **catch** handler, whichever is dynamically nearer to the exception context. For example, this sample C++ program raises a C exception inside a C++ **try** context:
16
17
17
18
## Example - Catch a C exception in a C++ catch block
18
19
@@ -51,7 +52,7 @@ Caught a C exception.
51
52
52
53
## C exception wrapper classes
53
54
54
-
In a simple example like the above, the C exception can be caught only by an ellipsis (**...**) **catch** handler. No information about the type or nature of the exception is communicated to the handler. While this method works, in some cases you may want to define a transformation between the two exception handling models so that each C exception is associated with a specific class. To do this, you can define a C exception "wrapper" class, which can be used or derived from in order to attribute a specific class type to a C exception. By doing so, each C exception can be handled separately by a specific C++ **catch** handler, instead of all of them in a single handler.
55
+
In a simple example like the above, the C exception can be caught only by an ellipsis (**...**) **catch** handler. No information about the type or nature of the exception is communicated to the handler. While this method works, in some cases you may want to define a transformation between the two exception handling models so that each C exception is associated with a specific class. To transform one, you can define a C exception "wrapper" class, which can be used or derived from in order to attribute a specific class type to a C exception. By doing so, each C exception can be handled separately by a specific C++ **catch** handler, instead of all of them in a single handler.
55
56
56
57
Your wrapper class might have an interface consisting of some member functions that determine the value of the exception, and that access the extended exception context information provided by the C exception model. You might also want to define a default constructor and a constructor that accepts an **unsigned int** argument (to provide for the underlying C exception representation), and a bitwise copy constructor. Here is a possible implementation of a C exception wrapper class:
57
58
@@ -72,7 +73,7 @@ public:
72
73
};
73
74
```
74
75
75
-
To use this class, install a custom C exception translation function that is called by the internal exception handling mechanism each time a C exception is thrown. Within your translation function, you can throw any typed exception (perhaps an `SE_Exception` type, or a class type derived from `SE_Exception`) that can be caught by an appropriate matching C++ **catch** handler. The translation function can simply return, which indicates that it did not handle the exception. If the translation function itself raises a C exception, [terminate](../c-runtime-library/reference/terminate-crt.md) is called.
76
+
To use this class, install a custom C exception translation function that is called by the internal exception handling mechanism each time a C exception is thrown. Within your translation function, you can throw any typed exception (perhaps an `SE_Exception` type, or a class type derived from `SE_Exception`) that can be caught by an appropriate matching C++ **catch** handler. The translation function can instead return, which indicates that it did not handle the exception. If the translation function itself raises a C exception, [terminate](../c-runtime-library/reference/terminate-crt.md) is called.
76
77
77
78
To specify a custom translation function, call the [_set_se_translator](../c-runtime-library/reference/set-se-translator.md) function with the name of your translation function as its single argument. The translation function that you write is called once for each function invocation on the stack that has **try** blocks. There is no default translation function; if you do not specify one by calling **_set_se_translator**, the C exception can only be caught by an ellipsis **catch** handler.
0 commit comments