Compaq AAQ2G1FTK Marine Radio User Manual


 
Handling Input/Output Exception Conditions
7.1 Planning for the AT END Condition
7.1 Planning for the AT END Condition
Compaq COBOL provides you the option of testing for this condition with the AT
END phrase of the READ statement (for sequential, relative, and indexed files)
and the AT END phrase of the ACCEPT statement.
Programs often read sequential files from beginning to end. They can produce
reports from the information in the file or even update it. However, the program
must be able to detect the end of the file, so that it can continue normal
processing at that point. If the program does not test for this condition when it
occurs, and if no applicable Declarative USE procedure exists (see Section 7.4),
the program terminates abnormally. The program must detect when no more
data is available from the file so that it can perform its normal end-of-job
processing and then close the file.
Example 7–1 shows the use of the AT END phrase with the READ statement for
sequential, relative, and indexed files.
Example 71 Handling the AT END Condition
READ SEQUENTIAL-FILE AT END PERFORM A600-TOTAL-ROUTINES
PERFORM A610-VERIFY-TOTALS-ROUTINES
MOVE "Y" TO END-OF-FILE.
READ RELATIVE-FILE NEXT RECORD AT END PERFORM A700-CLEAN-UP-ROUTINES
CLOSE RELATIVE-FILE
STOP RUN.
READ INDEXED-FILE NEXT RECORD AT END DISPLAY "End of file"
DISPLAY "Do you want to continue?"
ACCEPT REPLY
PERFORM A700-CLEAN-UP-ROUTINES.
7.2 Planning for the Invalid Key Condition
The INVALID KEY clause is available for the Compaq COBOL DELETE, READ,
REWRITE, START, and WRITE statements. (It does not apply to the READ
NEXT statement.) An invalid key condition occurs whenever the I/O system
cannot complete a DELETE, READ, REWRITE, START, or WRITE statement.
When the condition occurs, execution of the statement that recognized it is
unsuccessful, and the file is not affected.
For example, relative and indexed files use keys to access (retrieve or update)
records. The program specifying random access must initialize a key before
executing a DELETE, READ, REWRITE, START, or WRITE statement. If the
key does not result in the successful execution of any one of these statements, the
invalid key condition exists. This condition is fatal to the program, if the program
does not check for the condition when it occurs and if no applicable Declarative
USE procedure exists (see Section 7.4).
The invalid key condition, although fatal if not planned for, can be to your
advantage when used properly. You can, as shown in Example 7–2, read through
an indexed file for all records with a specific duplicate key and produce a report
from the information in those records. You can also plan for an invalid key
condition on the first attempt to find a record with a specified key value that is
not present in the file. In this case, planning for the invalid key condition allows
the program to continue its normal processing. You can also plan for the AT END
condition when you have read and tested for the last of the duplicate records
72 Handling Input/Output Exception Conditions