COBOL SIZE ERROR Phrase Example

Scenario1 - No overflow

Input-        WS-A = 700, WS-B = 200

Declaration-  05 WS-A     PIC 9(03) VALUE 700.
              05 WS-B     PIC 9(03) VALUE 200.
              05 WS-C     PIC 9(03).
			  
Code-         ADD WS-A, WS-B TO WS-C 
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.   
				
Result-       WS-C = 900

In the above case, the result is 900. So, the NOT ON SIZE ERROR phrase gets executed and displays the the result 900.

Scenario2 - Result overflow

Input-        WS-A = 900, WS-B = 200

Declaration-  05 WS-A     PIC 9(03) VALUE 900.
              05 WS-B     PIC 9(03) VALUE 200.
              05 WS-C     PIC 9(03).
			  
Code-         ADD WS-A, WS-B TO WS-C 
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.     
				
Result-       OVERFLOW

In the above case, the result is 1100. However, WS-C is declared as 9(3) and the maximum value can save is 999. So, the SIZE ERROR phrase gets executed and displays the message "OVERFLOW".


Previous Example
Subroutines (INLCUDE Stmt)
Next Example
EXIT Statement