Compute On Size Error Example


Scenario - Describes how to handle SIZE ERROR while using COMPUTE.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMPUTES. 
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INP1        PIC 9(3) VALUE 240.
          05 WS-INP2        PIC 9(2) VALUE 10.
          05 WS-OP          PIC 9(3).

       PROCEDURE DIVISION.

           COMPUTE WS-OP = WS-INP1 * WS-INP2
                       ON SIZE ERROR DISPLAY "SIZE ERROR"     
                   NOT ON SIZE ERROR DISPLAY "RESULT: " WS-OP 
           END-COMPUTE.

           STOP RUN.

Output -

SIZE ERROR

Explaining Example -

In the above example:

  • It multiplies two variables, WS-INP1 and WS-INP2.
  • The size error occured during the computation, it displays "SIZE ERROR".