The code in the programs can cause possible system or runtime problems. The error-handling code can anticipate the above problems and if this code not included, output data or files could be corrupted and the user might not even be aware that there is a problem.

The error-handling code can take actions such as handling the situation, issuing a message or halting the program execution. COBOL for AIX contains special elements to help and correct error conditions -

  • ON OVERFLOW in STRING and UNSTRING operations
  • ON SIZE ERROR in arithmetic operations
  • Elements for handling input or output errors
  • ON EXCEPTION or ON OVERFLOW in CALL statements

Handling errors in joining and splitting strings -

During the joining or splitting of strings, if the pointer used by STRING or UNSTRING might fall outside the range of the receiving field, a potential overflow condition exists, but COBOL does not let the overflow happen.

Instead, the STRING or UNSTRING operation is not completed, the receiving field remains unchanged and control passes to the next sequential statement.

If ON OVERFLOW phrase do not coded of the STRING or UNSTRING statement, user do not notify of the incomplete operation.

Handling errors in arithmetic operations -

The arithmetic operation results might be larger than the fixed-point data item to hold them or might have tried dividing by zero. In either case, the ON SIZE ERROR clause after the ADD, SUBTRACT, MULTIPLY, DIVIDE or COMPUTE statement can handle the situation.

For ON SIZE ERROR to work correctly for fixed-point overflow and decimal overflow, the TRAP(ON) runtime option must be set. The imperative statement of the ON SIZE ERROR clause gets performed and the result field won't change in these cases -

  • Fixed-point overflow
  • Division by zero
  • Zero raised to the zero power
  • Zero raised to a negative number
  • Negative number raised to a fractional power

Handling errors in input and output operations -

When an input or output operation fails, COBOL does not automatically take corrective action. User should choose whether the program will continue running after a less-than-severe input or output error. Below techniques can be used for intercepting and handling certain input or output conditions or errors -

  • End-of-file condition (AT END)
  • ERROR declaratives
  • FILE STATUS clause and file status key
  • File system status code
  • Imperative-statement phrases in READ or WRITE statements
  • INVALID KEY phrase

Handling errors when calling programs -

When a program dynamically calls a program, the called program might be unavailable and CALL statement does not have an ON EXCEPTION or ON OVERFLOW phrase, application program might abend.

ON EXCEPTION phrase should be used to perform a series of statements and to perform user-defined error handling. The ON EXCEPTION phrase applies only to the availability of the called program on its initial load. If the called program is loaded but fails for any other reason, the ON EXCEPTION phrase is not performed.