Handling Exceptions in the PAL

The SSCLI implementation uses Win32 Structured Exception Handling (SEH) heavily, and because of this, the PAL must provide an implementation of this feature for its use.

Before launching into the details of how the PAL implements SEH portably, a quick review is in order. SEH takes the following form in Win32 code:

    _  _try {
      <guarded code>
    }
    _  _except (<filter>) {
      <exception handler>
    }
    _  _finally {
      <termination handler>
    }

The filter and its exception handler are executed if an exception occurs during the execution of the guarded code; if an exception occurs within the try block, the filter is used to determine whether the except block should be run. The termination handler is an optional piece of code that is executed whenever control moves out of the guarded section; as control moves out of the try block for any reason, the finally block is executed. Any code blocks may themselves contain try blocks,exceptblocks, orfinally blocks (or call functions that contain such blocks), and because of this, handlers may be, and often are, nested to an arbitrary depth.

The algorithm for exception handling is as follows:

  1. An exception is raised.

  2. The system looks at the hierarchy of active exception handlers and executes the filter of the handler with highest precedence. This is the exception handler most recently installed and most deeply nested.

  3. If the filter passes control by returning EXCEPTION_CONTINUE_SEARCH, execution returns to step 1 but at the next highest precedence ...

Get Shared Source CLI Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.