Handling Nested Exceptions

Exceptions can be nested. A nested exception is an exception that occurs while another exception is being handled. When this happens, the processing of the first exception is temporarily suspended. Exception handling begins again with the most recently generated exception.
Note: If a nested exception causes the program to end, the exception handler for the first exception may not complete.

Example:

The following example shows a nested exception.
Figure 1. ILE C Source to Nest Exceptions
#include <signal.h>
void hdlr_hdlr(_INTRPT_Hndlr_Parms_T * __ptr128 parms)
{
   /* Handle exception 2 using QMHCHGEM. */
}
void main_hdlr(_INTRPT_Hndlr_Parms_T * __ptr128 parms)
{
#pragma exception_handler(hdlr_hdlr,0,0,_C2_MH_ESCAPE)
   /* Generate exception 2.              */
   /* Handle exception 1 using QMHCHGEM. */
}
int main(void)
{
#pragma exception_handler(main_hdlr,0,0,_C2_MH_ESCAPE)
   /* Generate exception 1.              */
}

In this example, the main() function generates an exception which causes main_hdlr to get control. The handler main_hdlr generates another exception which causes hdlr_hdlr to get control. The handler hdlr_hdlr handles the exception. Control resumes in main_hdlr, and it handles the original exception.

As this example illustrates, you can get an exception within an exception handler. To prevent exception recursion, exception handler call stack entries act like control boundaries with regards to exception percolation. Therefore it is recommended that you monitor for exceptions within your exception handlers.