ADD ON SIZE ERROR Example


Scenario - Addition error handling.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-A      PIC 9(03) VALUE 900.
          05 WS-B      PIC 9(03) VALUE 200.
          05 WS-C      PIC 9(03) VALUE 100.

       PROCEDURE DIVISION.
           ADD WS-A, WS-B TO WS-C
                ON SIZE ERROR DISPLAY "OVERFLOW"
            NOT ON SIZE ERROR DISPLAY "NOT OVERFLOW".

           STOP RUN.

Output -

OVERFLOW

Explaining Example -

In the above example:

  • It adds the values of WS-A and WS-B and stores the result in WS-C.
  • The result of the addition operation causes overflow (exceeding the size limit of the defined data type), so it displays "OVERFLOW".
  • After handling the exception, the program terminates.