Monday, November 21, 2011

File Error – Handling


File Error – Handling

There are chances for failure of any file I-O processing. The failure of an I-O operation can be accepted or cannot be tolerated. The severity of failure has to be defined in the program design stage.
          Let us assume that we don’t have any error handling in our program. In this case, for example, if you don’t have a specific record in the file, the random read of that record would immediately terminate the program with error ‘record not found’. 

Error Handling Clauses Provided by COBOL.
The sudden termination can be avoided by handling this error, with INVALID KEY clause of READ. Based on the importance of the record and business rule, we can continue our program with next record or terminate the program properly.
AT END is another error handling clause provided by COBOL. But there is no way to handle all such errors in this way.

Assign file-status and take the responsibility.
The second method is, assigning file-status to the file in the SELECT clause and checks the file status after each and every I-O and ensures that the value of status code is one of the allowable values. If it is not an allowable return code, then abnormally end the program with error statements that would be easier to debug.
But we have to do this checking after each and every I-O operation.
This is MOST PREFERRED ERROR HANDLING METHOD in structured programming.

Declaratives – USE statement

COBOL provides an option to group all the possible errors of specific operation(s) in a place and that will be automatically invoked during the respective operation(s) of any file. This avoids redundant code.
This is done in DECLARATIVE section of the procedure division. DECLARATIVE should be the first section in the procedure division if coded.

PROCEDURE DIVISION.
DECLARATIVES.
USE-PROCEDURE SECTION.
          USE AFTER EXCEPTION PROCEDURE ON INPUT
ERROR-PROCEDURE.
          Check the file-status code for validity.      
END-DECLARATIVES.
Whenever there is an error in the processing of ANY FILE opened in INPUT mode, then the control comes to ERROR-PROCEDURE. The validity of error should be checked in this paragraph and allow or restrict the process down, based on severity of error code.

The complete syntax of USE statements is:
USE AFTER STANDARD ERROR|EXCEPTION PROCEDURE ON
INPUT|OUTPUT|I-O|EXTEND| file-1
If INPUT is coded, the following procedure will be executed for every operation involved in any file that is opened in INPUT mode. OUTPUT, I-O and EXTEND have the same meaning but the mode is different.
If file name (file-1) is coded in the USE statement, then all the input-output operation of that specific file will be checked.
ERROR and EXCEPTION are synonyms.
The Procedure written in a DECLARATIVE section should not refer to any non-declarative procedure written after the end procedure and vice-versa.
 I-O-CONTROL - SAME AREA AND SAME RECORD AREA
RESERVE clause of SELECT statement specifies the number of buffers to be allocated for a file. SAME AREA allows more than one file to use the same buffer area. This will be very useful when the program must work with a limited memory space. But the problem is only one file should be open at a time if SAME AREA is coded.
Syntax: SAME AREA FOR file-1 file-2 file-3.

If SAME RECORD AREA is coded, then the buffer is not shared but only the record area is shared. So more than one file can be in open state. We should be careful while filling in the record area of the output file. This may destroy the record read most recently.
Syntax: SAME RECORD AREA FOR file-1 file-2 file-3.

SAME SORT AREA allows more than one sort/merge work files to use the same area. The sort work files are automatically allocated when file is opened and de-allocated when file is closed.  As the sort file is automatically opened and closed during a SORT and two sort files cannot be opened at a time, this clause may not be useful.
Syntax: SAME SORT|SORT-MERGE AREA for file-1 file-2.
   File-1 or file-2 should be a SD file.

I-O CONTROL- RERUN Clause

          RERUN ON rescue FOR EVERY integer RECORDS on file-1
This will cause checkpoint to be taken for every interger-1 records processing of
file-1. If the program ABENDED before the complete processing of the file-1, then the program will restart from integer+1ST record instead of first record. The rescue file details should be mentioned outside the program and it varies from installation to installation.

ENTRY statement
ENTRY statement establishes an alternate ENTRY point in a COBOL called
sub-program. When a CALL statement naming the alternate entry point is executed in a calling program, control is transferred to the next executable statement following the entry statement. Except when a CALL statement refers to an entry name, the ENTRY statements are ignored at run-time.

Matching Logic
If you have been given two files of similar type, say master and transaction file and you are requested to update the master file with transaction file information for existing records and prepare a report of new transactions and deleted transactions, then you should go for what is called Matching logic. This is also known as co-sequential processing.

Sort both the files on key and compare the keys. If the keys are matching then update the file. If you find any record that is found in transaction but not in master file, then that is new addition and the reverse is deletion. If the master key is greater than transaction key, then that corresponds to the first case and reverse is the second case.

This can be easily done in JCL using ICETOOL. Refer JCL section

No comments:

Post a Comment