Module 6: Procedure Division
ON SIZE ERROR option
- SIZE ERROR occurs in arithmetic operation when result value exceeds the size of the PICTURE specification of receiving field
- For example, DATA-A = 60 (PIC 99) and DATA-B = 60 (PIC 99). Now, arithmetic operation “ADD DATA-A TO DATA-B” will result DATA-B =20 even though the expected value is 120, but due to PIC specification of DATA-B, truncation occurred.
- Such types of truncation resulted due to SIZE error. Such size errors can be handled using ON SIZE ERROR option
- A size error condition exist when result is truncated on either left or right hand side
- If an arithmetic operation is coded with ROUNDED option then a size error occurs only if there is truncation on the left hand side.
- Division by zero can also be handled using ON SIZE ERROR option
- Basic syntax:-
Arithmetic statement [ON SIZE ERROR <imperative-statement>].
- If ON SIZE ERROR option evaluates true, imperative-statement will be executed
- The imperative statement needs to be ended either by period or scope terminator (for example, END-ADD is scope terminator for ADD statement)
In DATA DIVISION,05 A PIC 99 VALUE 20. 05 B PIC 99 VALUE 50. 05 C PIC 99 VALUE 80. 05 TOTAL PIC 99.In PROCEDURE DIVISION,ADD A B C GIVING TOTAL ON SIZER ERROR MOVE ZERO TO TOTAL.After execution of ADD statement, values in data items will be A=20, B=50, C=80, D =0. D will have zero because the ADD statement will try to store 150 which will results in SIZE ERROR and thus imperative statement coded after ON SIZE ERROR option will set D=0