An exception dispatcher provides you with information about the exception. This exception information can be accessed in various ways. There is a function that returns the exception code and a function that returns a pointer to the exception data structure.
To find the exception code of the exception that was raised, there is an intrinsic function called GetExceptionCode which returns a value identifying the exception. This exception number is defined in the file <excpt.h.>
DWORD GetExceptionCode(VOID).
GetExceptionCode can only be called in an Exception Filter or inside an exception handler. It cannot be called inside an Exception Filter function. Here is a legal way to call GetExceptionCode:
f(){
/* Code before try block */ (1)
__try{
/* Guarded body */ (2)
}
__except(
((GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
GetExceptionCode() == EXCEPTION_FLT_DIVIDE_BY_ZERO)
? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_EXECUTION))
) /* Exception Filter */ ){ (3)
/* Exception handler only*/ (4)
switch (GetExceptionCode(){
case EXCEPTION_ACCESS_VIOLATION:
/* handle access violation
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
/* handle divide by zero */
break;
}
}
/* Code after the exception handler */ (5)
}
Here is an illegal way of calling GetExceptionCode:
f(){
/* Code before try block */ (1)
__try{
/* Guarded body */ (2)
}
__except( filter() /* Exception Filter */ ){ (3)
/* Exception handler only*/ (4)
switch (GetExceptionCode(){
case EXCEPTION_ACCESS_VIOLATION:
/* handle access violation
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
/* handle divide by zero */
break;
}
}
/* Code after the exception handler */ (5)
}
LONG filter() {
/* Compile-time error */
return
((ExceptionCode()==EXCEPTION_ACCESS_VIOLATION||EXCEPTION_FLT_DIVIDE_BY_
ZERO)?EXCEPTION_EXECUTE_HANDLER:EXCEPTION_CONTINUE_EXECUTION);
}
To make the above function behave properly, simply rewrite the filter function with a parameter, as follows:
f(){
/* Code before try block */ (1)
__try{
/* Guarded body */
} (2)
__except( filter(GetExceptionCode()) /* Exception Filter */ ){ (3)
/* Exception handler only*/ (4)
switch (GetExceptionCode(){
case EXCEPTION ACCESS_VIOLATION:
/* handle access violation
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
/* handle divide by zero */
break;
}
}
/* Code after the exception handler */ (5)
}
LONG filter(DWORD exception_code) {
return
((exception_code==EXCEPTION_ACCESS_VIOLATION||EXCEPTION_FLT_DIVIDE_BY_
ZERO)?EXCEPTION_EXECUTE_HANDLER:EXCEPTION_CONTINUE_EXECUTION);
}
When an exception occurs, the exception dispatcher saves the information into a data structure called the Exception_Record. This data structure is accessed through another intrinsic function called GetExceptionInformation. It returns a pointer to a structure that contains a pointer to the Exception_Record.
LPEXCEPTION_POINTERS GetExceptionInformation(VOID);
This function can only be called in an exception filter, not an Exception Filter function, and not inside an exception handler. It returns a pointer to a data structure of two pointers to structures. One points to the Exception_Record and the other points to the Context. The Context structure describes the hardware state, such as register contents, at the time of the exception. This structure will differ depending on which machine you executed your code.
The Exception_Record structure is reproduced here:
typedef struct _EXCEPTION_RECORD {
DWORD ExceptionCode;
DWORD ExceptionFlags;
struct _EXCEPTION_RECORD *ExceptionRecord;
PVOID ExceptionAddress;
DWORD NumberParameters;
DWORD ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS];
} EXCEPTION_RECORD;
The Exception_Record and Context structures are all defined in <winnt.h>. ExceptionCode is the code of the exception and it is the same as the information that is returned by GetExceptionCode.
ExceptionFlags contains flags about the exception that is useful for the case EXCEPTION_CONTINUE_EXECUTION. Specifically, if an exception cannot be continued, then this flag will be set to EXCEPTION_NONCONTINUABLE_EXCEPTION. Any attempt to continue execution by returning EXCEPTION_CONTINUE_EXECUTION after a noncontinuable exception causes the EXCEPTION_NONCONTINUABLE_EXCEPTION exception to be generated.
Exception_Record points to an associated EXCEPTION_RECORD structure that may be chained when nested exception occurs. This means that an exception has occurred inside an exception handler.
ExceptionAddress has the address of the instruction that caused the exception to occur.
The last two members work together to offer additional information about the exception. NumberParameters gives the number of parameters in the ExceptionInformation array member that follows. Currently, only the exception EXCEPTION_ACCESS_VIOLATION will generate additional information. All other exceptions will have the NumberParameters member as zero. An access violation generates two additional pieces of information. This information is stored in the array ExceptionInformation. Element 0 will be set to 0 if the thread tried to read inaccessible data, and 1 if the thread tried to write to an inaccessible address. Element 1 specifies the virtual address of the inaccessible data.
![]()
Structured Exception Handling
Exception Handler
Exception Handler - Global
Unwind
