Subtract On Size Error Example


Scenario - Subtract error handling.

Code -

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

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

       PROCEDURE DIVISION.
           SUBTRACT WS-A FROM WS-B
                    GIVING WS-C 
                    ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.

           STOP RUN.

Output -

OVERFLOW

Explaining Example -

In the above example:

  • It defines three numeric variables: WS-A, WS-B, and WS-C, with WS-A initialized to 200, WS-B initialized to 900, and WS-C left uninitialized.
  • It subtracts the value of WS-A from WS-B and stores the result in WS-C.
  • If an overflow occurs during this subtraction, it displays "OVERFLOW"; otherwise, it displays the value of WS-C.