Structured exception handling is a mechanism for handling both hardware and software exceptions. The key advantage of SEH is that is allows the programmer to treat all exceptions in an uniform manner.
Vectored Exception Handling (VEH) is an extension to structured exception handling. The key advantage of VEH is that is allows the programmer to register one or more handlers in an application.
An exception is an event that occurs during the execution of a program, and requires the execution of code outside the normal flow of control. There are two types of exceptions.
- Hardware exceptions are initiated by the CPU.
- Software exceptions are initiated explicitly by applications or the operating system. That is, user- and kernel-mode exceptions.
An exception can be continuable or noncontinuable. A noncontinuable exception arises when the event is not continuable in the hardware, or if continuation makes no sense. A noncontinuable exception does not terminate the application.
Structured Exception Handling
The Microsoft C/C++ Optimizing Compiler provides the following keywords:
-
__try
keyword that identifies a guarded body of code. This statement identifies a guarded body of code. -
__except
keyword that identifies an exception handler. This statement identifies a filter expression. -
__finally
keyword that identifies a termination handler. This statement identifes code that is always executed whenever the flow of control leaves a guarded body of code, regardless of whether the guarded body terminated normally or abnormally.
These keywords are for frame-based exceptions. A frame-based exception handler allows you to deal with the possibility that an exception may occur in a certain sequence of code. It consists of the following elements.
-
A guarded body of code. The guarded body of code is a set of one or more statements for which the filter expression and the exception-handler block provide exception-handling protection. The Microsoft C/C++ Optimizing Compiler, a guarded body is enclosed by braces ({}) following the
__try
keyword. -
A filter expression. The filter expression of a frame-based exception handler is an expression that is evaluated by the system when an exception occurs within the guarded body.
-
An exception-handler block. With an exception-handler block, in contrast, execution continues sequentially from the exception handler rather than from the point of the exception.
__try
{
// guarded code
}
__except ( filter_expression )
{
// exception handler code
}
__finally
{
// termination code that is always executed when flow of control exits the guarded code
}
A filter expression has the following effect.
- The system stops its search for an exception handler, restores the machine state, and continues thread execution at the point at which the exception occurred.
- The system continues its search for an exception handler.
- The system transfers control to the exception handler, and thread execution continues sequentially in the stack frame in which the exception handler is found.
Vectored Exception Handling
Vectored exception handlers are an extension to structured exception handling. An application can register a function to watch or handle all exceptions for the application. Vectored handlers are not frame-based, therefore, you can add a handler that will be called regardless of where you are in a call frame.