Menu
CoddyTech

Guarding Against S0C7

Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 12 of 16.

A numeric field must hold digits. When ADD or COMPUTE meets one that does not, the program stops with a data exception: an S0C7 ABEND on a mainframe, one of the most common in batch work. GnuCOBOL stops with a message like this one:

error: 'TR-AMOUNT' (Type: NUMERIC DISPLAY)
  not numeric: '00012A0'

Bad digits come from outside the program: a record with a letter or spaces in an amount, a file written with another layout. They also come from inside: a numeric field with no VALUE starts as binary zeros, which are not digits either.

The class test IS NUMERIC is true only when every byte of the field is a digit. Test before the arithmetic, and handle the bad record instead of stopping the run:

IF TR-AMOUNT IS NUMERIC
    ADD TR-AMOUNT TO TOTAL
ELSE
    DISPLAY "Skipped " TR-ID
END-IF

ACCEPT into a number never complains: GnuCOBOL quietly turns 12A into 12. Accept typed text into a PIC X field and test it. 42 in a PIC X(5) is 42 and three spaces, so it is numeric only when full: 00042.

challenge icon

Challenge

Easy

The program totals the amounts in TRANSIN, but record 10003 holds 00012A0 and 10004 has no amount, so the run stops with a data exception. Add the amount only when it IS NUMERIC, and print Skipped and the id otherwise.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TOTALS.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT TRANS-FILE ASSIGN TO TRANSIN
               ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD TRANS-FILE.
       01 TRANS-REC.
           05 TR-ID          PIC 9(5).
           05 TR-AMOUNT      PIC 9(5)V99.
       WORKING-STORAGE SECTION.
       01 EOF-FLAG       PIC X VALUE "N".
           88 END-OF-FILE  VALUE "Y".
       01 TOTAL          PIC 9(5)V99 VALUE ZERO.
       01 SHOW-TOTAL     PIC ZZ,ZZ9.99.
       PROCEDURE DIVISION.
           OPEN INPUT TRANS-FILE
           PERFORM UNTIL END-OF-FILE
               READ TRANS-FILE
                   AT END SET END-OF-FILE TO TRUE
                   NOT AT END
                       ADD TR-AMOUNT TO TOTAL
               END-READ
           END-PERFORM
           CLOSE TRANS-FILE
           MOVE TOTAL TO SHOW-TOTAL
           DISPLAY "Total: " SHOW-TOTAL
           STOP RUN.

All lessons in Programs That Work Together