### Write or Register an Exception Handler

To write an exception handler, you must use operating system-specific APIs, and conform to the prototype required by the operating system.

See system documentation for details on writing Windows exception handlers.

You can register an exception handler in two ways: using an OS/2 API, or using preprocessor directives.

When you register a handler using #pragma handler, the chain is updated for you and the following tasks, that you would otherwise have to code using APIs, are done automatically:

- The handler is registered before the function runs.
- The OS/2 structure that contains information about the exception, EXCEPTIONREGISTRATIONRECORD, is generated and attached to a chain in the thread information block
- The handler is deregistered when the function ends.

Calling OS/2 APIs gives you more flexibility than using #pragmas. Use this method when you want to:

- Register more than one exception handler in a module.
- Register an exception handler over only part of a function.
- Register more than one exception handler for a function.
- Make EXCEPTIONREGISTRATIONRECORD part of a larger structure, and access its fields inside the exception handler from the larger structure.

If you register your own exception handler, the exceptions you handle are not seen by a signal handler. The exceptions you do not handle are passed to the next exception handler. If the next handler is the IBM C and C++ Compilers default handler, _Exception, it converts the exception to a signal and calls the appropriate signal handler.

Signals and Exceptions
Signal and Exception Handling

Register an Exception Handler Using Preprocessor Directives
Register an Exception Handler Using OS/2 APIs

Example of an OS/2 Exception Handler
Prototype of an OS/2 Exception Handler
Signal and Exception Handling Considerations