ILE COBOL Programmer's Guide

Detecting End-of-File Conditions (AT END Phrase)

An end-of-file condition may or may not represent an error. In many designs, reading sequentially to the end of a file is done intentionally, and the AT END condition is expected.

In some cases, however, the end-of-file condition will reflect an error. You code the AT END phrase of the READ statement to handle either case, according you your program design.

If you code an AT END phrase, the imperative statement identified by the phrase is performed when an end-of-file condition occurs. If you do not code an AT END phrase, the associated USE AFTER EXCEPTION/ERROR declarative is performed.

Any NOT AT END phrase that you code is performed only if the READ statement completed successfully. If the READ operation fails because of any condition other than end-of-file, neither the AT END nor the NOT AT END phrase is performed. Instead, control passes to the end of the READ statement after performing the associated USE AFTER EXCEPTION/ERROR declarative procedure.

If you have coded neither an AT END phrase nor a USE AFTER EXCEPTION/ERROR declarative procedure, but you have coded a STATUS KEY clause for the file, control passes to the next sequential instruction after the input-output statement that detected the end-of-file condition. At this point, your code should look at the status key and take some appropriate action to handle the error.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]