Syntax of Termination Handlers
The structure of a termination handler is:
__try {
statement-block-1
}
__finally {
statement-block-2
}
The statements in statement-block-1 are executed unconditionally. The statements in statement-block-2 are always executed, in one of two ways:
If statement-block-1 finishes execution normally, statement-block-2 is then executed.
If statement-block-1 is prematurely terminated for any reason, including a jump out of the block, the system executes statement-block-2 as a part of the process of unwinding the stack.
In the second case, the function returns TRUE if called from within statement-block-2; otherwise, it returns FALSE.
If statement-block-1 was abnormally terminated, the termination handler is executed as a part of the process of unwinding the stack. If control jumps outside of several blocks of code at once, the system clears the stack frame for each block of code or function exited, starting with the lowest (most deeply-nested) stack frame. As each frame is cleared, the system executes its termination handler.
For example, suppose a series of function calls links function A to function D, as shown in the following figure. Each function has one termination handler. If an exception is raised in function D and handled in A, the termination handlers are called in this order as the system unwinds the stack: D, C, B.
Order of Termination-Handler Execution