ILE COBOL Programmer's Guide

Detecting Invalid Key Conditions (INVALID KEY Phrase)

The imperative statement identified by the INVALID KEY phrase will be given control in the event that an input-output error occurs because of a faulty index key or relative key. You can include INVALID KEY phrases on READ, START, WRITE, REWRITE, and DELETE statements for indexed and relative files.

The INVALID KEY phrases differ from USE AFTER EXCEPTION/ERROR declaratives in these ways:

If you specify the INVALID KEY phrase in an input-output statement that causes an invalid key condition, control is transferred to the imperative statement identified by the INVALID KEY phrase. In this case, any USE AFTER EXCEPTION/ERROR declaratives you have coded are not performed.

Any NOT INVALID KEY phrase that you specify is performed only if the statement completes successfully. If the operation fails because of any condition other than invalid key, neither the INVALID KEY nor NOT INVALID KEY phrase is performed. Instead, control passes to the end of the input-output statement after performing any associated USE AFTER EXCEPTION/ERROR declaratives.

Use the FILE STATUS clause in conjunction with the INVALID KEY phrase to evaluate the status key and determine the specific invalid key condition.

For example, assume you have a file containing master customer records and you need to update some of these records with information in a transaction update file. You will read each transaction record, find the corresponding record in the master file, and make the necessary updates. The records in both files each contain a field for a customer number, and each record in the master file has a unique customer number.

The FILE-CONTROL entry for the master file of commuter records includes statements defining indexed organization, random access, MASTER-COMMUTER-NUMBER as the prime record key, and COMMUTER-FILE-STATUS as the file status key. The following example illustrates how you can use the FILE STATUS clause in conjunction with the INVALID KEY phrase to more specifically determine the cause of an input-output statement failure.

     .
     . (read the update transaction record)
     .
     MOVE "TRUE" TO TRANSACTION-MATCH
     MOVE UPDATE-COMMUTER-NUMBER TO MASTER-COMMUTER-NUMBER
     READ MASTER-COMMUTER-FILE INTO WS-CUSTOMER-RECORD
        INVALID KEY
           DISPLAY "MASTER CUSTOMER RECORD NOT FOUND"
           DISPLAY "FILE STATUS CODE IS: " COMMUTER-FILE-STATUS
           MOVE "FALSE" TO TRANSACTION-MATCH
     END-READ


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