ON SIZE ERROR
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 17 of 23.
A result too big for its field is a size error. Without a check, COBOL cuts the digits and goes on:
01 TOTAL PIC 9(3) VALUE 999.
ADD 1 TO TOTAL. *> 000ON SIZE ERROR runs a statement instead, and the receiving field keeps its old value:
ADD 1 TO TOTAL
ON SIZE ERROR DISPLAY "Too big"
END-ADD.Dividing by zero is a size error too. Without ON SIZE ERROR, GnuCOBOL leaves the result field as it was and goes on; an IBM mainframe stops the program with an S0CB abend.
NOT ON SIZE ERROR runs when the result fits. END-ADD, END-DIVIDE and the other END- words close the statement:
DIVIDE A BY B GIVING Q
ON SIZE ERROR
DISPLAY "Cannot divide"
NOT ON SIZE ERROR
DISPLAY Q
END-DIVIDE.Challenge
EasyRead TOTAL and PEOPLE, divide to get SHARE-AMT, and display Share: 025. When PEOPLE is 0, display Cannot split by zero instead.
Example input, one line per ACCEPT: 100, 4.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. SHARES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TOTAL PIC 9(3).
01 PEOPLE PIC 9(2).
01 SHARE-AMT PIC 9(3).
PROCEDURE DIVISION.
ACCEPT TOTAL.
ACCEPT PEOPLE.
*> Divide, or say "Cannot split by zero"
STOP RUN.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.