COBOL

COBOL

COBOL

COBOL is responsible for the efficient, reliable, secure, and unseen day-to-day operations of the world's economy.

 View Only
  • 1.  Try/Catch in COBOL

    Posted Thu March 07, 2024 10:45 AM

    Hello,

    I tried the COBOL example of 'try/catch' I copied from this page : CEEMRCE-Move resume cursor explicit - IBM Documentation but it doesn't work.

    I compiled the 3 programs and submited pgm2 using a JCL.

    pgm2 abended here : COMPUTE ANSWER = 1 / ANSWER. with S0CB (Decimal-Divide Exception) and the handler UCH1 was not used.

    I guess I did something wrong but no idea what.

    Any suggestion welcome.

    /Loic



    ------------------------------
    Loic Vital-Durand
    ------------------------------


  • 2.  RE: Try/Catch in COBOL

    Posted Tue March 12, 2024 12:33 PM

    Hello Loic,

    I will get back to you in a few days. I remember trying some codes based on the example and worked fine. We will figure out. 



    ------------------------------
    Roy Bae
    ------------------------------



  • 3.  RE: Try/Catch in COBOL

    Posted Tue March 12, 2024 12:54 PM

    I will try to find an answer for you and also update the example if needed.  Thank you so much for the feedback!



    ------------------------------
    Debbie Noll
    ------------------------------



  • 4.  RE: Try/Catch in COBOL

    Posted Wed March 13, 2024 01:26 PM
      |   view attached

    Hello Loic,

    I tried the example in the link and it worked fine for me. There were some formatting errors causing compile time errors which I had to fix myself though - sorry about that. Regardless, I found the example can be improved and will work with Debbie (IBM) to get that done later. In the meantime, I came up with a simple version below that would give you the idea about how try/catch works and help you test and debug the functionality in your environment. Could you try it and let me know how it goes? 

    (Note that I also attached 'error_recovery_le.txt'  (plain text) that contains the same contents below for your convenience)

    ------------------------------------------------------------

    1. An example of CEEMRCE called by COBOL:

    PGM1 registers a condition handler (UCH1) using CEEHDLR and sets a resume point using CEE3SRP. When a condition occurs (in this example, ANSWER = 1 / 0), UCH1 gets a control to handle the condition and resumes the program execution to the resume point in PGM1.

    To try the following example, compile PGM1 and UCH1, and statically linked them together to generate a program object (load module) named PGM1. Then run PGM1.

    Module/File Name: PGM1

           CBL NODYNAM
           IDENTIFICATION DIVISION.
           PROGRAM-ID. PGM1.
          *Module/File Name: PGM1
          *------------------------------------------*
          * Sample program using CEE3SRP and CEEMRCE.*
          * PGM1 registers user-written condition    *
          * handler UCH1 using CEEHDLR.  It          *
          * sets a resume point using CEE3SRP.       *
          *------------------------------------------*
           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01  RECOVERY-AREA EXTERNAL.
               05  RECOVERY-POINT            POINTER.
               05  ERROR-INDICATOR           PIC X(01).
           01 UCH-ROUTINE      PROCEDURE-POINTER.
           01  FIELDS.
               05  FIRST-TIME-SW    PIC X(03) VALUE ' ON'.
                   88 FIRST-TIME-88           VALUE ' ON'.
               05  ANSWER    PIC S9(01) COMP-3 VALUE 0.
               05  TOKEN     PIC S9(09) BINARY.
               05  FC.
                   10  CASE-1.
                       15  SEVERITY PIC S9(04) BINARY.
                       15  MSG-NO   PIC S9(04) BINARY.
                   10  SEV-CTL      PIC  X(01).
                   10  FACILITY-ID  PIC  X(03).
                   10  I-S-INFO     PIC S9(09) BINARY.
     
           PROCEDURE DIVISION.
               DISPLAY "PGM1: === BEGINS ==="
               SET UCH-ROUTINE TO ENTRY 'UCH1'.
          *------------------------------------------*
          *    Register the condition handler, UCH1. *
          *------------------------------------------*
               CALL 'CEEHDLR' USING UCH-ROUTINE, TOKEN, FC.
               IF CASE-1 NOT = LOW-VALUE
                   DISPLAY "PGM1: ERROR: CALL CEEHDLR FAILED"
                   GOBACK.
               DISPLAY "PGM1: === CALL COMPUTE-LOOP 2 TIMES ==="
               DISPLAY " "
               PERFORM COMPUTE-LOOP 2 TIMES.
               SET RECOVERY-POINT TO NULL.
               DISPLAY "PGM1: === ENDS ==="
               GOBACK.
     
            COMPUTE-LOOP.
               DISPLAY "PGM1: COMPUTE-LOOP BEGINS"
               IF FIRST-TIME-88
                   MOVE 'OFF' TO FIRST-TIME-SW
          *------------------------------------------*
          *        Set up a resume point.            *
          *------------------------------------------*
                   CALL 'CEE3SRP' USING RECOVERY-POINT, CASE-1
                   SERVICE LABEL
                   IF CASE-1 NOT = LOW-VALUE
                       DISPLAY "PGM1: ERROR: CALL CEE3SRP FAILED"
                       GOBACK
                   END-IF
                   display "PGM1: RESUME POINT"
               END-IF
     
               IF ERROR-INDICATOR = 'E'
                   MOVE SPACE TO ERROR-INDICATOR
                   MOVE 1 TO ANSWER
               END-IF
     
          * Application code may go here.
               DISPLAY "PGM1: BEFORE DOING 1 / " ANSWER.
               COMPUTE ANSWER = 1 / ANSWER.
               DISPLAY "PGM1: AFTER  DOING 1 / " ANSWER.
     
               DISPLAY "PGM1: COMPUTE-LOOP ENDS"
               DISPLAY " "
               EXIT.
           END PROGRAM PGM1.

    Module/File Name: UCH1

           CBL NODYNAM APOST
           IDENTIFICATION DIVISION.
           PROGRAM-ID. UCH1.
          *Module/File Name: UCH1
          *------------------------------------------*
          * Sample user condition handler using      *
          * CEEMRCE.  This program sets an error     *
          * flag for the program-in-error to query   *
          * and issues a call to CEEMRCE to return   *
          * control to the statement following the   *
          * call to CEE3SRP.                         *
          *------------------------------------------*
           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01  RECOVERY-AREA EXTERNAL.
               05 RECOVERY-POINT         POINTER.
               05 ERROR-INDICATOR        PIC X(01).
           01  FC.
               10  CASE-1.
                   15  SEVERITY PIC S9(04) BINARY.
                   15  MSG-NO   PIC S9(04) BINARY.
               10  SEV-CTL      PIC  X(01).
               10  FACILITY-ID  PIC  X(03).
               10  I-S-INFO     PIC S9(09) BINARY.
     
           LINKAGE SECTION.
           01  CURRENT-CONDITION         PIC  X(12).
           01  TOKEN                     PIC  X(04).
           01  RESULT-CODE             PIC S9(09) BINARY.
               88  RESUME                 VALUE +10.
               88  PERCOLATE              VALUE +20.
               88  PERC-SF                VALUE +21.
               88  PROMOTE                VALUE +30.
               88  PROMOTE-SF             VALUE +31.
           01  NEW-CONDITION             PIC  X(12).
     
           PROCEDURE DIVISION  USING CURRENT-CONDITION,
                                     TOKEN,
                                     RESULT-CODE,
                                     NEW-CONDITION.
               DISPLAY "      > UCH1: RECOVERY BEGINS"
               DISPLAY "      > UCH1: MOVE E TO ERROR-INDICATOR"
               MOVE 'E' TO ERROR-INDICATOR.
          *------------------------------------------*
          *    Call CEEMRCE to return control to the *
          *    last resume point.                    *
          *------------------------------------------*
               CALL 'CEEMRCE' USING RECOVERY-POINT, FC.
               IF CASE-1 NOT = LOW-VALUE
                   DISPLAY "      > UCH1: ERROR: CALL CEEMRCE FAILED"
                   GOBACK.
               MOVE +10 TO RESULT-CODE.
               DISPLAY "      > UCH1: GO BACK TO RESUME POINT"
               GOBACK.
           END PROGRAM UCH1.

    Actual program output

    PGM1: === BEGINS ===
    PGM1: === CALL COMPUTE-LOOP 2 TIMES ===
     
    PGM1: COMPUTE-LOOP BEGINS
    PGM1: RESUME POINT
    PGM1: BEFORE DOING 1 / 0
          > UCH1: RECOVERY BEGINS
          > UCH1: MOVE E TO ERROR-INDICATOR
          > UCH1: GO BACK TO RESUME POINT
    PGM1: RESUME POINT
    PGM1: BEFORE DOING 1 / 1
    PGM1: AFTER  DOING 1 / 1
    PGM1: COMPUTE-LOOP ENDS
     
    PGM1: COMPUTE-LOOP BEGINS
    PGM1: BEFORE DOING 1 / 1
    PGM1: AFTER  DOING 1 / 1
    PGM1: COMPUTE-LOOP ENDS
     
    PGM1: === ENDS ===


    ------------------------------
    Roy Bae
    ------------------------------

    Attachment(s)

    txt
    error_recovery_le.txt   6 KB 1 version


  • 5.  RE: Try/Catch in COBOL

    Posted Wed March 13, 2024 01:34 PM

    In response to your question, by the way, in my case, 

    • I compiled PGM2 and UCH1 separately, and statically linked them together. Created a program object(load module) named PGM2
    • I compiled and linked PGM3. Created PGM3
    • At execution, I made sure a dataset containing PGM3 is accessible. (for example, STEPLIB=ROYBAE.COBOL.LOAD). Then I ran PGM2

    Do you see any major difference between mine and your set-up?



    ------------------------------
    Roy Bae
    ------------------------------



  • 6.  RE: Try/Catch in COBOL

    Posted Wed March 27, 2024 11:54 AM

    Hello Roy,

    I had some tips from some of your colleague (don't know his name).

    The issue was due to that we use TRAP(OFF,SPIE) by default.

    So I tried again adding

    //CEEOPTS DD *
    TRAP(ON,SPIE)

    to my JCL and it works !

    Another day in paradise. Thank you so much.

    /Loic



    ------------------------------
    Loic Vital-Durand
    ------------------------------



  • 7.  RE: Try/Catch in COBOL

    Posted Wed March 27, 2024 12:36 PM

    Hello Loic,

    It's great news. I should've checked TRAP(ON,SPIE) - I learn something new everyday. When improving the doc (  CEEMRCE-Move resume cursor explicit - IBM Documentation  ),  I will make sure to mention about the option. Thanks for the feedback!



    ------------------------------
    Roy Bae
    ------------------------------