Handling a divide-by-zero condition in C, C++, COBOL, or PL/I

Figure 1 and the following examples provide an illustration of how user-written condition handlers can handle conditions such as a divide-by-zero in a C, C++, COBOL, or PL/I application. In the C or C++ examples in C or C++ handling a divide-by-zero condition, the COBOL examples in COBOL handling a divide-by-zero condition, and the PL/I examples in PL/I handling a divide-by-zero condition, the main routine calls CEEHDLR to register the user-written condition handler (USRHDLR program (COBOL) for COBOL). The main routine then calls the DIVZERO routine (Figure 1 for COBOL), in which a divide-by-zero exception occurs.

Figure 1. Handle and resume cursor movement as a condition is handled
The main routine calls CEEHDLR to register the user-written condition handler.

Divide-by-zero is enabled as a condition in the following steps:

  1. The handle cursor, which first points at DIVZERO's stack frame, moves down the stack to the USRHDLR condition handler, the first user-written condition handler established to handle conditions for the main routine's stack frame.
  2. For divide-by-zero conditions, USRHDLR issues a call to CEEMRCR (Move Resume Cursor Relative to Handle Cursor) with a 0 type_of_move, meaning move the resume cursor to the call return point of the stack frame associated with the handle cursor. (The call return point is the next instruction after the call to the DIVZERO routine.)
  3. Execution resumes in the main routine at this point. A divide-by-zero condition is the only type of program interrupt for which USRHDLR causes a resume.
  4. All other program interrupts are percolated to the next condition handler on the stack.

For simplicity, the examples shown in this topic do not include calls to some Language Environment services that could also be useful for handling conditions in your application. For example, you might code in the USRHDLR routine a call to the CEE3GRN callable service in order to get the name of the routine that incurred the condition.