Win32 SEH exception can be captured as .NET exceptions. In the following code, the managed function (main) native function call (F), which throws SEH exception exception_INT_DIVIDE_BY_ZERO. In general, this exception is captured in the processing exception ExceptionA System :: type catch block.
// ExceptionHandling2.cpp // compile with "cl /clr ExceptionHandling2.cpp" // As I will discuss later, #pargma managed is not recommended; it is only // used to show exceptions thrown across managed / unmanaged boundaries // without using two source files #pragma unmanaged int f() { #pragma managed int main() { catch (System::ExceptionA ex) { System::Console::WriteLine(ex->GetType()->FullName);
If compile and execute the application, the type name System.DivideByZeroException catch block writes. Most SEH exception code maps to the type System :: Runtime :: InteropServices :: SEH exception. System.DivideByZeroException is mapped to one of the few special cases. Table 7-4 shows the existence of special mapping of SEH exception.
Win32 Exception Code |
Hex Value |
Managed Exception |
||
EXCEPTION_ |
ACCESS_VIOLATION |
C0000005 |
System: |
:AccessViolationException |
EXCEPTION_ |
NO_MEMORY |
C0000017 |
System: |
:OutOfMemoryException |
EXCEPTION_ |
ARRAY_BOUNDS_EXCEEDED |
C000008C |
System: |
: IndexOutOfRangeException |
EXCEPTION_ |
FLT_DENORMAL_OPERAND |
C000008D |
System: |
:FormatException |
EXCEPTION |
FLT_DIVIDE_BY_ZERO |
C000008E |
System: |
:DivideByZeroException |
EXCEPTION_ |
FLT_INEXACT_RESULT |
C000008F |
System: |
:ArithmeticException |
EXCEPTION_ |
FLT_INVALID_OPERATION |
C0000090 |
System: |
:ArithmeticException |
EXCEPTION_ |
FLT_OVERFLOW |
C0000091 |
System: |
:OverflowException |
EXCEPTION_ |
FLT_STACK_CHECK |
C0000092 |
System: |
:ArithmeticException |
EXCEPTION_ |
FLT_UNDERFLOW |
C0000093 |
System: |
:ArithmeticException |
EXCEPTION_ |
INT_DIVIDE_BY_ZERO |
C0000094 |
System: |
:DivideByZeroException |
EXCEPTION_ |
INT_OVERFLOW |
C0000095 |
System: |
:OverflowException |
EXCEPTION_ |
STACK_OVERFLOW |
C00000FD |
System: |
:StackOverflowException |
All other SEH exceptions |
System::Runtime:: InteropServices::SEHException |
As shown in the above table, an access violation (0xC0000005) are automatically mapped to System :: AccessViolationException. This type of exception has been introduced in the .NET 2.0. In earlier versions of .NET, it will lead to System :: null reference exception. Because this is a disruptive change, you can switch back to the original behavior, configuration files as follows:
<configuration> <runtime> <legacyNullReferenceExceptionPolicy enabled="1"/> </runtime> </configuration>