COBOL condition handling examples

The following examples demonstrate how conditions are handled in Language Environment if you do not use any Language Environment callable services, and do not have any user-written condition handlers registered. The COBOLA program in Figure 1 calls COBOLB in Figure 2, which in turn calls the COBOLC program, in Figure 3. A divide-by-zero condition occurs in COBOLC.

The divide-by-zero is enabled as a condition, so the condition step of Language Environment condition handling is entered. There is no user-written condition handler that is registered for COBOLC or any of the other COBOL programs, so the condition is percolated through all of the stack frames. COBOL's default action for the divide-by-zero condition is to percolate the condition to Language Environment. The divide-by-zero condition has a severity of 3. The Language Environment default response to an unhandled severity 3 condition is to terminate the application and issue a message if TERMTHDACT(MSG) is specified.

Figure 1. COBOLA program
CBL LIB,QUOTE,NODYNAM
      *Module/File Name: IGZTMLTA
      *************************************************************
      *                                                           *
      * Demonstrate a failing COBOL program with multiple active  *
      * routines on the stack. The call sequence is as follows:   *
      *                                                           *
      * COBOLA --> COBOLB --> COBOLC (which causes a zero divide) *
      *                                                           *
      *************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COBOLA.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       1   Y  PIC 999 VALUE ZERO.
      *
       PROCEDURE DIVISION.
           DISPLAY "In COBOLA.".
           CALL "COBOLB" USING Y.

           GOBACK.

Figure 2 calls COBOLC and passes data to it.

Figure 2. COBOLB program
CBL LIB,QUOTE,NOOPTIMIZE,NODYNAM
      *************************************************
      *                                               *
      *  IBM Language Environment                     *
      *                                               *
      *  Licensed Materials - Property of IBM         *
      *                                               *
      *  5645-001 5688-198                            *
      *  (C) Copyright IBM Corp. 1991, 1997           *
      *  All Rights Reserved                          *
      *                                               *
      *  US Government Users Restricted Rights - Use, *
      *  duplication or disclosure restricted by GSA  *
      *  ADP Schedule Contract with IBM Corp.         *
      *                                               *
      *************************************************
      *Module/File Name:  IGZTMLTB
      *************************************************************
      *                                                           *
      * Second routine called in the following call sequence:     *
      *                                                           *
      * COBOLA --> COBOLB --> COBOLC (which causes a zero divide) *
      *                                                           *
      *************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COBOLB.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       1   X  PIC 999 VALUE ZERO.
       LINKAGE SECTION.
       1   Y  PIC 999.
      *
       PROCEDURE DIVISION USING Y.
           DISPLAY "In COBOLB.".
           MOVE Y TO X.
           CALL "COBOLC" USING X.

           GOBACK.

Figure 3 generates a divide-by-zero condition. The divide-by-zero condition is percolated back to COBOLB, to COBOLA, and to Language Environment default behavior.

Figure 3. COBOLC program
CBL LIB,QUOTE,NODYNAM
      *Module/File Name: IGZTMLTC
      *************************************************************
      *                                                           *
      * Third routine called in the following call sequence:      *
      *                                                           *
      * COBOLA --> COBOLB --> COBOLC (which causes a zero divide) *
      *                                                           *
      *************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COBOLC.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       LINKAGE SECTION.
       1   Y  PIC 999.
      *
       PROCEDURE DIVISION USING Y.
           DISPLAY "In COBOLC.".
           COMPUTE Y = 1 / Y.

           GOBACK.