To handle a file error or exception you can write a file error (INFSR) subroutine. When a file exception occurs:
A file error subroutine can handle errors in more than one file.
The following restrictions apply:
To add a file error subroutine to your program, you do the following steps:
Figure 138 shows an example of exception handling using an INFSR error subroutine. The program TRNSUPDT is a simple inventory update program. It uses a transaction file TRANSACT to update a master inventory file PRDMAS. If an I/O error occurs, then the INFSR error subroutine is called. If it is a record lock error, then the record is written to a backlog file. Otherwise, an inquiry message is issued.
Note that the File specification for PRDMAS identifies both the INFDS and identifies the INFSR to be associated with it.
The following is done for each record in the TRANSACT file:
*=================================================================*
* TRNSUPDT: This program is a simple inventory update program. *
* The transaction file (TRANSACT) is processed consecutively. *
* The product number in the transaction is used as key to access *
* the master file (PRDMAS) randomly. *
* 1. If the record is found, the quantity of the inventory will *
* be updated. *
* 2. If the record is not found, an error will be printed on a *
* report. *
* 3. If the record is currently locked, the transaction will be *
* written to a transaction back log file which will be *
* processed later. *
* 4. Any other unexpected error will cause a runtime error *
* message. *
*=================================================================*
*-----------------------------------------------------------------*
* Define the files: *
* 1) PRDMAS - Product master file *
* 2) TRANSACT - Transaction file *
* 3) TRNBACKLG - Transaction backlog file *
* 2) PRINT - Error report. *
*-----------------------------------------------------------------*
FPRDMAS UF E K DISK
F INFSR(PrdInfsr)
F INFDS(PrdInfds)
FTRANSACT IP E DISK
FTRNBACKLG O E DISK
FPRINT O F 80 PRINTER
*-----------------------------------------------------------------*
* Define the file information data structure for file PRDMAS. *
* The *STATUS field is used to determine what action to take. *
*-----------------------------------------------------------------*
D PrdInfds DS
D PrdStatus *STATUS
*-----------------------------------------------------------------*
* List of expected exceptions. *
*-----------------------------------------------------------------*
D ErrRecLock C CONST(1218)
*-----------------------------------------------------------------*
* Access the product master file using the transaction product *
* number. *
*-----------------------------------------------------------------*
C TRNPRDNO CHAIN PRDREC 10
*-----------------------------------------------------------------*
* If the record is found, update the quantity in the master file. *
*-----------------------------------------------------------------*
C IF NOT *IN10
C SUB TRNQTY PRDQTY
C UPDATE PRDREC
*-----------------------------------------------------------------*
* If the record is not found, write to the error report *
*-----------------------------------------------------------------*
C ELSE
C EXCEPT NOTFOUND
C ENDIF
C SETON LR
*-----------------------------------------------------------------*
* Error handling routine. *
*-----------------------------------------------------------------*
C PrdInfsr BEGSR
*-----------------------------------------------------------------*
* If the master record is currently locked, write the transaction *
* record to the back log file and skip to next transaction. *
*-----------------------------------------------------------------*
C PrdStatus DSPLY
C IF (PrdStatus = ErrRecLock)
C WRITE TRNBREC
C MOVE '*GETIN' ReturnPt 6
*-----------------------------------------------------------------*
* If unexpected error occurs, cause inquiry message to be issued. *
*-----------------------------------------------------------------*
C ELSE
C MOVE *BLANK ReturnPt
C ENDIF
C ENDSR ReturnPt
*-----------------------------------------------------------------*
* Error report format. *
*-----------------------------------------------------------------*
OPRINT E NOTFOUND
O TRNPRDNO
O 29 'NOT IN PRDMAS FILE'
When control is passed to the error subroutine, the following occurs:
Note that the check for a record lock error is done by matching the *STATUS subfield of the INFDS for PRDMAS against the field ErrRecLock which is defined with the value of the record lock status code. The INFSR could be extended to handle other types of I/O errors by defining other errors, checking for them, and then taking an appropriate action.