### Register an Exception Handler Using Preprocessor Directives

To explicitly register _Exception for a function, you can use the #pragma handler directive before the function definition. This directive generates the code to register the exception handler before the function runs, and to remove the exception handler when the function ends. For example, use the following directive if the name of the function for which the exception handler is to be registered is function:

>     #pragma handler(function)

You can register your own exception handler in place of _Exception, using two preprocessor directives:

>     #pragma map(_Exception, "MyExceptHandler")
>     #pragma handler(myfunc)

The #pragma map directive tells the compiler that all references to the name _Exception are to be mapped to MyExceptHandler. Because of the name mapping, MyExceptHandler is actually registered. The handler is registered on function entry and deregistered on exit; you cannot register the handler over only part of a function.

You can only use #pragmas to register one exception handler per module. You may need to place functions in separate modules to get the exception handling you want.

Due to the way OS/2 chains exception handlers, you gain flexibility by using OS/2 APIs to register your exception handler. However using #pragma handler is simpler because exceptions registered by the #pragma are chained automatically.

Signals and Exceptions

Write or Register an Exception Handler