An exception can be hardware or software generated. Users can generate software exception using the RaiseException() system call. In the past, one may have had to handle hardware exceptions differently than software exceptions. This has been unified under SEH.
Exception handling is performed using the try-except block.
/* Code before try block */ (1)
__try{
/* Guarded body */ (2)
}
__except( filter /* Exception Filter */ ){ (3)
/* Exception handler only*/ (4)
}
/* Code after the exception handler */ (5)
You need to use the header <excpt.h> which contains the data structure definitions for an exception. There can be no code between the try and the except or finally block. A try block also can only have one finally or except block associated with it. The actual order of code movement depends on a number of issues. The situation illustrated above is a case where there is an exception in the Guarded Body, and the Exception Filter evaluates to EXCEPTION_EXECUTE_HANDLER. If no exception occurs in the Guarded Body, then step 3 and 4 are omitted.
The Exception Filter is evaluated for one of three possible conditions. The Exception Filter itself can be a function, a constant, or an expression. The three options it can evaluate to are:
| EXCEPTION_EXECUTE_HANDLER | 1 |
| EXCEPTION_CONTINUE_EXECUTION | -1 |
| EXCEPTION_CONTINUE_SEARCH | 0 |
If your filter catches an exception that it is expecting after comparing it with the exception signals in <winnt.h>, the filter should return 1 (EXCEPTION_EXECUTE_HANDLER). This will cause your exception handler to be executed. After the exception handler is executed, control resumes at the first instruction after the except block.
If your filter evaluates to -1 (EXCEPTION_CONTINUE_EXECUTION), then you are indicating that you have taken care of the exception and that the execution can continue from the instruction that generated the exception and try again. Usually, the exception is repaired in the filter, and -1 is returned. However, this is not guaranteed to always work exactly as expected as it depends on the machine encoding. More than one instruction may be used to encode a single line of code, and the specific machine instruction that caused the exception may not have a value that has been repaired. This may cause an infinite loop.
The third possibility is a return value of 0 (EXCEPTION_CONTINUE_SEARCH) which means that you do not want to handle the exception. The exception dispatcher will look for another exception handler by searching backwards through the call stack. It will specifically look for the most-recently executing try block that's associated with an except block and ignore any try blocks that are matched with finally blocks. If none is found, then it will call the system exception handler for an unhandled exception.
![]()
Structured Exception Handling
Exception Handler - Global Unwind
Exception Handler - Exception Information
Exception Handler - RaiseException
