Understanding Exception Filters and Exception Handlers by Example

[Previous] [Next]

Unlike termination handlers (discussed in the previous chapter), exception filters and exception handlers are executed directly by the operating system—the compiler has little to do with evaluating exception filters or executing exception handlers. The next several sections illustrate the normal execution of try-except blocks, explain how and why the operating system evaluates exception filters, and show the circumstances under which the operating system executes the code inside an exception handler.

Funcmeister1

Here's a more concrete coding example of a try-except block:

 DWORD Funcmeister1() { DWORD dwTemp; // 1. Do any processing here.     _ _try { // 2. Perform some operation. dwTemp = 0; } _ _except (EXCEPTION_EXECUTE_HANDLER) { // Handle an exception; this never executes.     } // 3. Continue processing. return(dwTemp); } 

In the Funcmeister1 try block, we simply move a 0 into the dwTemp variable. This operation will never cause an exception to be raised, so the code inside the except block will never execute. Note this difference from try-finally behavior. After dwTemp is set to 0, the next instruction to execute is the return statement.

Although return, goto, continue, and break statements are strongly discouraged in the try block of a termination handler, no speed or code-size penalty is associated with using these statements inside the try block of an exception handler. Such a statement in the try block associated with an except block won't incur the overhead of a local unwind.

Funcmeister2

Let's modify the function and see what happens:

 DWORD Funcmeister2() { DWORD dwTemp = 0; // 1. Do any processing here.     _ _try { // 2. Perform some operation(s). dwTemp = 5 / dwTemp; // Generates an exception dwTemp += 10; // Never executes } _ _except ( /* 3. Evaluate filter. */ EXCEPTION_EXECUTE_HANDLER) { // 4. Handle an exception. MessageBeep(0);        } // 5. Continue processing. return(dwTemp); } 

In Funcmeister2, an instruction inside the try block calls for the attempt to divide 5 by 0. The CPU will catch this event and raise a hardware exception. When this exception is raised, the system will locate the beginning of the except block and evaluate the exception filter expression, an expression that must evaluate to one of the following three identifiers as defined in the Windows' Excpt.h file.

Identifier Defined As
EXCEPTION_EXECUTE_HANDLER 1
EXCEPTION_CONTINUE_SEARCH 0
EXCEPTION_CONTINUE_EXECUTION -1

In the next few sections, we'll discuss how each of these identifiers alters the thread's execution. While reading these sections, you can refer to Figure 24-1, which summarizes how the system processes an exception.

click to view at full size.

Figure 24-1. How the system processes an exception



Programming Applications for Microsoft Windows
Programming Applications for Microsoft Windows (Microsoft Programming Series)
ISBN: 1572319968
EAN: 2147483647
Year: 1999
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net