DIVIDE On Size Error Example


Scenario - Error handling while using DIVIDE.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-VAR1      PIC 9(03) VALUE 500.
          05 WS-VAR2      PIC 9(03) VALUE 5.
          05 WS-RESULT    PIC 9(02).

       PROCEDURE DIVISION.

           DIVIDE WS-VAR1
             INTO WS-VAR2 
           GIVING WS-RESULT
                  ON SIZE ERROR DISPLAY "OVERFLOW"
              NOT ON SIZE ERROR DISPLAY "RESULT: " WS-VAR3
           END-DIVIDE.
           STOP RUN.

Output -

OVERFLOW

Explaining Example -

In the above example:

  • Three variables declared in the working-storage section: WS-VAR1, WS-VAR2, and WS-RESULT.
  • WS-VAR1 and WS-VAR2 are initialized with the values 500 and 5 respectively..
  • It performs a division operation where WS-VAR1 is divided by WS-VAR2, and the result is stored in WS-RESULT.
  • The division operation encounters a size error (WS-RESULT can hold upto 99, however the result is 100, which is overflow), it displays "OVERFLOW".