ON SIZE ERROR
A field of two digits cannot hold a hundred, and the language gives the program a place to say what to do about it. ON SIZE ERROR is a branch of the arithmetic statement, taken when the result does not fit, and the branch is the only moment at which the program can notice.
A result that does not fit
Ninety-nine plus one in a field of two digits, with the branch and its opposite on the statement:
IDENTIFICATION DIVISION.
PROGRAM-ID. SIZE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SMALL-F PIC 9(2) VALUE 99.
01 WIDE-F PIC 9(4) VALUE 100.
PROCEDURE DIVISION.
ADD 1 TO SMALL-F
ON SIZE ERROR DISPLAY "small overflow, value still " SMALL-F
NOT ON SIZE ERROR DISPLAY "small ok " SMALL-F
END-ADD.
ADD 1 TO WIDE-F
ON SIZE ERROR DISPLAY "wide overflow"
NOT ON SIZE ERROR DISPLAY "wide ok " WIDE-F
END-ADD.
STOP RUN.
cobc -x -free -Wall -o size size.cob
./size
small overflow, value still 99
wide ok 0101
The two statements run in one program so that the two branches are both visible: the first takes the error branch because a hundred does not fit in two digits, the second takes the other because a hundred and one fits in four. The compiler cannot know either answer, since the values are data; the test is made at run time, on the result, before the field is written.
The field keeps its old value
The first output line says more than it seems: the field still holds 99. A statement that goes to its error branch does not write a partial result, so the receiving field is exactly as it was before:
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPUTE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 QTY-F PIC 9(4) VALUE 2000.
01 PRICE-F PIC 9(3)V99 VALUE 9.99.
01 TOTAL-F PIC 9(4)V99 VALUE 7.00.
PROCEDURE DIVISION.
COMPUTE TOTAL-F = QTY-F * PRICE-F
ON SIZE ERROR DISPLAY "compute overflow, total [" TOTAL-F "]"
NOT ON SIZE ERROR DISPLAY "total [" TOTAL-F "]"
END-COMPUTE.
STOP RUN.
cobc -x -free -Wall -o compute compute.cob
./compute
compute overflow, total [0007.00]
Two thousand at nine ninety-nine is nineteen thousand nine hundred and eighty, which needs five integer digits and the field has four. The total is still seven, the value it had, so the error branch can print it and mean "this is the last good total" rather than "this is the failed computation".
Without the clause the value wraps
Take the clause away and the same addition succeeds with a wrong result:
IDENTIFICATION DIVISION.
PROGRAM-ID. SILENT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SMALL-F PIC 9(2) VALUE 99.
PROCEDURE DIVISION.
ADD 1 TO SMALL-F.
DISPLAY "value now [" SMALL-F "]".
STOP RUN.
cobc -x -free -Wall -o silent silent.cob
./silent
value now [00]
The build is clean, the run exits with status zero, and the field holds zero. This is the failure mode that costs a company money: not a crash but a plausible number, printed in a report and reconciled a month later. A program that adds to a counter without an ON SIZE ERROR has no way to notice the day the counter reaches its maximum.
A division by zero takes the branch
The clause covers more than an overflow. A division by zero has no result, and the statement treats that as a size error:
IDENTIFICATION DIVISION.
PROGRAM-ID. ZERO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ZERO-F PIC 9(2) VALUE 0.
01 AMT-F PIC 9(5)V99 VALUE 100.00.
01 OUT-F PIC 9(5)V99 VALUE 7.00.
PROCEDURE DIVISION.
DIVIDE ZERO-F INTO AMT-F GIVING OUT-F
ON SIZE ERROR DISPLAY "division by zero, out still " OUT-F
NOT ON SIZE ERROR DISPLAY "out " OUT-F
END-DIVIDE.
STOP RUN.
cobc -x -free -Wall -o zero zero.cob
./zero
division by zero, out still 00007.00
Seven is still seven. The statement reports the error instead of writing zero, which is what the lesson before this shows happening without the clause.
Where the clause goes
The clause belongs to one statement and ends with the statement: ON SIZE ERROR, the statements of the branch, an optional NOT ON SIZE ERROR with its own statements, and END-ADD, END-COMPUTE, END-DIVIDE, END-MULTIPLY or END-SUBTRACT.
A period in place of the scope terminator ends the sentence and with it the branch, which is how a statement meant to be guarded ends up unguarded. Two other things are worth knowing. The check is on the receiving field, so a result that fits is never an error even if an intermediate value is larger; and ROUNDED is applied before the check, so a rounded value that fits is fine.
Exercises
- Put the first program in a loop that adds one to the small field ten times and prints a message on the size error. How many additions succeed before the branch is taken?
- Give the silent program an ON SIZE ERROR that moves the field to a wider one and adds there. What is the total now?
- Divide by zero without the clause, then with it, and compare the two outputs with the two outputs of this lesson. Which line tells the reader that something went wrong?
- Compute a product that fits the receiver only after ROUNDED is applied, and one that fits before it. Which of the two takes the error branch?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
When is the ON SIZE ERROR branch of an arithmetic statement taken?
Answer
When the result does not fit the receiving field
The test is made on the value about to be stored, against the picture of the field that would store it.
-
What does the receiving field hold after the error branch runs?
Answer
The value it had before the statement
The statement does not write a partial result, so the error branch sees the last good value and can report it.
-
What happens to 99 + 1 in a two-digit field with no error clause?
Answer
It wraps to 00 and nothing is reported
Without the clause the arithmetic succeeds with the low digits of the result, which is the quiet failure the clause exists for.
-
Which arithmetic error besides an overflow takes the ON SIZE ERROR branch?
Answer
A division by zero
A zero divisor is a size error: the statement reports it and leaves the receiving field as it was.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 SMALL-F PIC 9(2) VALUE 99. PROCEDURE DIVISION. ADD 1 TO SMALL-F ON SIZE ERROR DISPLAY "overflow " SMALL-F NOT ON SIZE ERROR DISPLAY "ok " SMALL-F END-ADD. STOP RUN.Answer
overflow 99
A hundred does not fit two digits, so the error branch runs and reads the field that still holds its old value.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WIDE-F PIC 9(4) VALUE 100. PROCEDURE DIVISION. ADD 1 TO WIDE-F ON SIZE ERROR DISPLAY "overflow" NOT ON SIZE ERROR DISPLAY "ok " WIDE-F END-ADD. STOP RUN.Answer
ok 0101
A hundred and one fits four digits, so the receiving field is written and printed in its full width.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 SMALL-F PIC 9(2) VALUE 99. PROCEDURE DIVISION. ADD 1 TO SMALL-F. DISPLAY "[" SMALL-F "]". STOP RUN.Answer
[00]
Without the clause the result wraps at a hundred, so the two-digit field holds the low digits and prints 00.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 ZERO-F PIC 9(2) VALUE 0. 01 AMT-F PIC 9(5)V99 VALUE 100.00. 01 OUT-F PIC 9(5)V99 VALUE 7.00. PROCEDURE DIVISION. DIVIDE ZERO-F INTO AMT-F GIVING OUT-F ON SIZE ERROR DISPLAY "[" OUT-F "]" END-DIVIDE. STOP RUN.Answer
[00007.00]
The division by zero takes the error branch, and the receiving field still holds the seven it had.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 QTY-F PIC 9(4) VALUE 2000. 01 PRICE-F PIC 9(3)V99 VALUE 9.99. 01 TOTAL-F PIC 9(4)V99 VALUE 7.00. PROCEDURE DIVISION. COMPUTE TOTAL-F = QTY-F * PRICE-F ON SIZE ERROR DISPLAY "[" TOTAL-F "]" END-COMPUTE. STOP RUN.Answer
[0007.00]
The product needs five integer digits, so the error branch runs and the total is still the seven it started with.
-
A program computes a total without ON SIZE ERROR and the field is too small. What does the operator see in the report?
Answer
A plausible but wrong number, with no message anywhere
The wrap of the result happens silently, which is why a missing error clause turns an overflow into a reporting bug.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev