Handling errors in input and output operations

When an input or output operation fails, COBOL does not automatically take corrective action. You choose whether your program will continue running after a less-than-severe input or output error.

About this task

You can use any of the following techniques 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

    For VSAM files, if you specify a FILE STATUS clause, you can also test the VSAM status code to direct your program to error-handling logic.

  • INVALID KEY phrase

To have your program continue, you must code the appropriate error-recovery procedure. You might code, for example, a procedure to check the value of the file status key. If you do not handle an input or output error in any of these ways, a severity-3 Language Environment® condition is signaled, which causes the run unit to end if the condition is not handled.

The following figure shows the flow of logic after a VSAM input or output error:

This image shows the logic flow after a VSAM I/O error.
Link to detail.

The following figure shows the flow of logic after an input or output error with QSAM or line-sequential files. The error can be from a READ statement, a WRITE statement, or a CLOSE statement with a REEL/UNIT clause (QSAM only).

This image shows the logic flow after a QSAM I/O error.
Link to detail.

*Possible phrases for QSAM are AT END, AT END-OF-PAGE, and INVALID KEY; for line sequential, AT END.

**You need to write the code to test the file status key.

***Execution of your COBOL program continues after the input or output statement that caused the error.

Related references  
File status key (Enterprise COBOL for z/OS® Language Reference)