Termination Handler - Local Unwind (C)

There are two ways of leaving a try block -- normal termination and abnormal termination. When the code executes to the closing brace, the try block is said to have exited normally. If it exited because of a raised exception, goto, return, break, continue, or longjmp, then the try block is said to have exited abnormally.

Abnormal termination causes a Local Unwind to occur. A Local Unwind occurs when the compiler executes the finally body, then executes the abnormal exit statement. A Local Unwind can also be caused by a Global Unwind which we will cover in a subsequent section. But for the context of this discussion, a Global Unwind is also an abnormal termination.

The basics of an abnormal exit can be illustrated using a goto statement inside a try-finally block:

   /* Code before try block */                                  (1)

  __try{
  /* Guarded body */                                           (2)
  goto label;
  /*  back here to execute the abnormal exit */     (4)
  }

  __finally{
  /* termination handler */                                    (3)
  }

  /* Code directly after the termination handler will not be executed*/
  /* Code after the label will be executed */
  label:
  /* Code after the label */                                   (5)

From the above code ordering, it is not surprising that the compiler generates instructions to remember to execute the goto (or return, break/continue, exception handler or longjmp) after the execution of the finally block. This is a performance penalty that always occurs in a Local Unwind and, as such, it should be avoided when possible.



Structured Exception Handling
Termination Handler
Termination Handler - Leave and AbnormalTermination