Vous êtes sur la page 1sur 12

1

FILE ERROR HANDLING

DECLARATIVES , FILE STATUS Clause and the USE procedure can be


used to handle all Exception / Error conditions. Sometimes a bulky program
may terminate due to error in a few records . To avoid this Declaratives can
be used . The actual process is like , the operating system moves the value
of the FILE STATUS into the variable associated to the FILE STATUS , whenever
an input-output statement is executed . The required action in case of
exception is specified through a USE procedure . The paragraph after the
USE procedure is executed incase of any errors.
DECLARATIVES : is used to specify the set of statements which is to be
performed when an error occurred while operating on the specified file.
USE: statement can contain either the file-name itself or the mode of file ie
INPUT, OUTPUT, etc.
DECLARATIVE SECTION : Whenever any Error occurs in a file the control
goes to the declarative section . It is defined as first section of the
PROCEDURE DIVISION.
The DECLARATIVE SECTION pertaining to the USE AFTER statement will
automatically be executed by the computer whenever an Input or Output
error has occurred .
Syntax :
DECLARATIVES.
Section-name SECTION.
USE AFTER STANDARD (EXCEPTION / ERROR) PROCEDURE
ON file-name ]
END DECLARATIVES.
Eg :
DECLARATIVES.
DECL SECTION.
USE AFTER STANDARD ERROR PROCEDURE ON FILE-NAME(S)
Imperative statement
END DECLARATIVES
FILE STATUES : The File status is used to trap errors during any operation is
done into the file. Whenever any error occurs in a file during any I-O
Operation , the specified file status variable is automatically updated.
FILE STATUS IS WS-FILE-STATUS
WORKING-STORAGE-SECTION.
01 WS-FILE-STATUS
PIC X(2).
File Status Clause : Used to monitor for errors and exceptional
conditions.

is used to monitor the processing of each I/O request for the file. When
specified system moves a value (which indicates the status of execution of
the statement) into the data item after each input output that implicitly
orexplicitly refers to the file.
File Status Codes :
02 -- Duplicate key was detected on READ statement was successfully
executed . This is not an error when our program includes WITH DUPLICATES
for the Key RECORD KEY clause)
04 -- Length of the record didnt confirm to the File Description
Specifications.
(fixed length attributes )
10 --No more input records . A Sequential READ statement ( READ
AT END) has been attempted , but there are no more input records.
(INVALID KEY)
21 Sequence error exists for a sequentially accessed Keyed file .
The keys are not in the correct order .
22 An attempt was made to write a record that would create a
Duplicate Primary Record key in a Keyed file.
23 An attempt was made to randomly access a record that does not
exist in the file . The required record was not found during a READ.
24 A boundary error has occurred . An attempt has been made to
write beyond the preestablished boundaries of a Keyed file. Space
not found to WRITE.
30- A Permanent hardware problem has occurred and no further
information is available .
34 A boundary error for a Sequential file has occurred . An attempt
was made to write beyond the externally described boundaries of a
sequential file.
37 An OPEN statement was attempted on a file that would not
support the mode specified in the OPEN statement ( Eg : Keyed file is
opened as OUTPUT when ACCESS IS RANDOM has been specified
Logic error condition
41 An OPEN statement has been attempted on a file that is already
open .
42 A CLOSE statement has been attempted on a file that has not
been opened
43 - An attempt was made to DELETE or REWRITE a record after an
unsuccessful READ (eg there is no record in memory to DELETE or
REWRITE .
47 The execution of a READ or START statement was attempted on a
file not opened as Input or I-O.
48 The execution of a WRITE statement was attempted on a Keyed
file not opened as I-O or OUTPUT.

49- A DELETE or REWRITE operation was attempted on a file not


opened as I-O.
9X Codes 90 99 are specifically defined for the AS/400 and
normally provide CPF error messages .

DECLARATIVES and USE example :


PROCEDURE DIVISION.
DECLARATIVES.
INPUT-FILE-ERROR SECTION.
USE AFTER STANDARD ERROR PROCEDURE ON INPUT-FILE.
INPUT-FILE-ERROR-PARA.
MOVE INPUT-FILE-STATUS TO SK.
MOVE "INPUT-FILE" TO FILE-NAME.
DISPLAY "FILE PROCESSING ERROR".
DISPLAY ERROR-INFO.
DISPLAY "PROCESSING TERMINATED DUE TO I-O ERROR".
STOP RUN.
I-O-FILE-ERROR SECTION.
USE AFTER STANDARD ERROR PROCEDURE ON MASTER-FILE.
MASTER-FILE-ERROR-PARA.
MOVE MASTER-FILE-STATUS TO SK.
MOVE "MASTER-FILE" TO FILE-NAME.
DISPLAY "FILE PROCESSING ERROR".
DISPLAY ERROR-INFO.
DISPLAY "PROCESSING TERMINATED DUE TO I-O ERROR".
STOP RUN.
END DECLARATIVES.

ENVIRONMENT DIVISION
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DISPLAY-FILE ASSIGN TO WORKSTATION-RBSF
ORGANIZATION IS TRANSACTION
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS SUBFILE-REL-NO
FILE STATUS IS STATUS-RB
WORKING-STORAGE SECTION.
01 ERRHDL-PARAM.
05 STATUS-RB PIC X(2).
88 SUBFILE-IS-FULL VALUE 9M

4
PROCEDURE DIVISION.
DECLARATIVES.
TRANSACTION-ERROR SECTION.
USE AFTER STANDARA ERROR PROCEDURE DISPLAY-FILE
ERROR-HANDLER.
IF SUBFILE-IS-FULL THEN
NEXT SENTENCE
ELSE
DISPLAY WORK-STATION ERROR STATUS-DSP.
END-DECLARATIVES.

Commitment control is a function that allows us to define and process a


number of changes to database file as a single unit (Transaction ).
COMMITMENT CONTROL
To make the commitment control work we need to define the file in I-O-CONTROL.
When all the changes can be made permanent to the file, we issue a COMMIT or else if
all the changes should not reflect in the physical file, we issue a ROLLBACK.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ACCOUNT-FILE ASSIGN TO DATABASE-ACCTMST
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD IS EXTERNALLY-DESCRIBED-KEY
FILE STATUS IS ACCOUNT-FILE-STATUS.
.
.
I-O-CONTROL.
COMMITMENT CONTROL FOR ACCOUNT-FILE.
.
.
.PROCEDURE DIVISION.
MAIN-PARA.
OPEN I-O ACCOUNT-FILE.
.
.
IF <condition> THEN
COMMIT
ELSE
ROLLBACK
END-IF.

I-O Control : It specifies when check points to be taken and storage areas that are
shared by different files.
Journaling is a function which records the changes in a file in a Journal .
These record images are used to recover the changes in the files should the
system end abnormally .
When need Commintemetn Control : Two situations present special
problems in database applications 1) Transactiions that update multiple
records and2) concurrent updating of database files by multiple jobs .
Journaling helps avoid lost updates .
Commitment control is used to protect the integrity of database and help
recover from abnormal system termination.
COMIT operation guarantees that the group of operations is completed .
Rollback operation guarantees that the group of operations is backed out.

LOCAL DATA AREA :The system automatically creates a local data area for each job.
The local data area is defined outside the COBOL program as an area of 1024 bytes.
A COBOL program can access the Local Data Area for its job with the ACCEPT and
DISPLAY statements , using a mnemonic name associated with the function-name
LOCAL-DATA.
when a job is submitted , the submitting jobs Local data area is copied into the
submitted Jobs local data area ..if there is no submitting job, the local data area is
initialized to blanks.
There is only one local data area associated with each job . Even if several workstations
are acquired by a single job , only one data area exists for that job . there is not a local
data area for each workstation.
It is used to pass any desired information between programs in a job . This information
may be free-form data , such as information messages , or may consist of a fully
structured or formatted set of fields.
ENVIRONMENT DIVISION.
SPECIAL-NAMES.
LOCAL-DATA IS LOCAL-DATA-AREA.
WORKING-STORAGE SECTION.
01 WS-DATA-AREA PIC X(20).
PROCEDURE DIVISION.
To Read the data area and store the values in a data item.
ACCEPT WS-DATA-AREA FROM LOCAL-DATA-AREA.
To Write into the Local Data area
DISPLAY WS-DATA-AREA UPON LOCAL-DATA-AREA.

SPECIAL-NAMES.
LOCAL-DATA IS LOCAL-DATA-AREA
REQUESTOR IS OPERATOR
WORKING-STORAGE SECTION.
77
WK-LOCAL
PIC X(1024)
PROCEDURE DIVISION.
ACCEPT WK-LOCAL FROM LOCAL-DATA-AREA

ENVIRONMENT DIVISION
FILE-CONTROL
SELECT temp-file ASSIGN TO device-type file-name
ORGANIZATION IS {1. INDEXED
2. SEQUENTIAL
3. RELATIVE
4. TRANSACTION}
ACCESS MODE IS {

1. SEQUENTIAL
2. RANDOM
3. DYNAMIC}
FILE STATUS IS identifier-1.
FILE SECTION
FD TEMP-FILE.
BLOCK CONTAINS 150 CHARACTERS (USED ONLY FOR TAPE FILES)
RECORD CONTAINS 50 CHARACTERS (SIZE OF EACH RECORD)
LABEL RECORDS ARE STANDARD (ONLY FOR DOCUMENTATION)
DATA RECORD IS RECORD-FORMAT.
01 RECORD-FORMAT.
02
ITEMS PIC
.

Instead of defining the record-format we can even copy the record-format from the database file
itself using COPY statement.
FILE HANDLING
1) OPEN
OPEN

mode

file-name-1

7
mode
Ex :

OPEN INPUT

file-name-2
NEW-FILE.

2 ) CLOSE
CLOSE file-name-1, file-name-2 .
3) READ
SEQUENTIAL RETRIEVAL USING SEQUENTIAL ACCESS
Syntax:
READ file-name NEXT RECORD [INTO identifier-1]
[WITH NO LOCK]
[FORMAT IS {1. identifier-2
2. literal-1}]
[AT END imperative-statement-2]
[NOT AT END imperative-statement-2]
[END-READ]

Example:
READ

TEMP-FILE NEXT RECORD INTO RECORD-1


AT END MOVE
B1 TO EOF.

SEQUENTIAL RETRIEVAL USING DYNAMIC ACCESS


Syntax:
READ

file-name {1. NEXT


RECORD
2. FIRST
3. LAST
4. PRIOR}
INTO identifier-1
[WITH NO LOCK]
[FORMAT IS {1. identifier-2
2. literal-1}]
[AT END imperative-statement-2]
[NOT AT END imperative-statement-2]
[END-READ]
Example:
READ TEMP-FILE PRIOR RECORD
AT END MOVE B1 TO BOF.
RANDOM RETRIEVAL
Syntax:
READ file-name RECORD INTO identifier-1
[WITH NO LOCK]
[KEY IS data-name-1]
[FORMAT IS {1. identifier-2
2. literal-1}
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-READ]

8
Example:
READ TEMP-FILE
KEY IS STUID OF TEMP-FILE
INVALID KEY DISPLAY not present
NOT INVALID KEY DISPLAY present
END-READ.
WRITE
SEQUENTIAL
Syntax:
WRITE record-name-1 FROM identifier-1
{1. BEFORE
ADVANCING {1. identifier-2
{1. LINE
2. AFTER}
2. mnemonic-name-1 2. LINES}
3. integer-1
4. PAGE}
AT {1. END-OF-PAGE imperative-statement-1
2. EOP}
NOT AT {1. END-OF-PAGE imperative-statement-1
2. EOP}
END-WRITE.
Example:
WRITE NEWREC FROM TEMPREC
AT END-OF-PAGE DISPLAY page overflow
NOT AT EOP DISPLAY continue u can still write more records
END-WRITE.
INDEXED FILES
Syntax:
WRITE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-WRITE.
Example:
WRITE NEW-REC FROM TEMP-REC
FORMAT IS ADDREC
INVALID KEY DISPLAY error in write statement
END-WRITE.
RELATIVE FILES
Syntax:
WRITE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
{1. INDICATORS
{1. IS
identifier-3
2. INDICATOR 2. ARE}
3. INDIC}

9
AT {1. EOP
2. END-OF-PAGE}
NOT AT {1. EOP
2. END-OF-PAGE}

imperative-statement-1
imperative-statement-2

Example:
WRITE NEW-REC
FORMAT IS ADDREC
INDICATORS ARE INDIC-AREA.

START
Syntax:
START file-name KEY IS {1. EQUAL TO
2. =
3. GREATER THAN
4. >
5. NOT LESS THAN
6. NOT <
7. GREATER THAN OR EQUAL TO
8. >=}
{1. EXTERNALLY-DESCRIBED KEY
2. data-item-1}
FORMAT IS {1. identifier-1
2. literal-1}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
Example:
START EMPLOYEE KEY IS EQUAL TO
EXTERNALLY-DESCRIBED KEY
INVALID KEY DISPLAY Employee does not exist
NOT INVALID KEY DISPLAY Employee found
END-START.

REWRITE
Syntax:
REWRITE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-REWRITE.
Example:
REWRITE NEW-REC
FORMAT IS EDTREC.
DELETE
Syntax:

10
DELETE filename RECORD
FORMAT IS identifier-1
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-DELETE.
Example:
DELETE TEMP-FILE
FORMAT IS TEMP-REC
INVALID KEY DISPLAY DELETION NOT DONE
NOT INVALID KEY DISPLAY DELETED SUCCESSFUL
END-DELETE.

SUBFILES
READ
Syntax:
READ SUBFILE filename
NEXT MODIFIED RECORD
INTO indentifier-1
FORMAT IS {1. identifier-2
2. literal-1}
TERMINAL IS {1. identifier-3
2. literal-2}
{1. INDICATORS
{1. IS
identifier-4
2. INDICATOR 2. ARE}
3. INDIC}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
AT END imperative-statement-3
NOT AT END imperative-statement-4
END-READ
Example:
READ SUBFILE EMPLOYEE
NEXT MODIFIED RECORD
FORMAT IS EMPSFL
INDICATORS ARE INDIC-AREA
END-READ.
WRITE
Syntax:
WRITE SUBFILE record-name-1 FROM I dentifier-1
FORMAT IS {1. identifier-2
2. literal-1}
TERMINAL IS {1. identifier-3
2. literal-1}
{1. INDICATORS
{1. IS
identifier-4
2. INDICATOR 2. ARE}
3. INDIC}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-WRITE.

11

Example:
WRITE SUBFILE EMPREC
FORMAT IS EMPSFL
INDICATORS ARE INDIC-AREA
INVALID KEY DISPLAY ERROR IN RRN VALUE
END-WRITE.

REWRITE
Syntax:
REWRITE SUBFILE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
TERMINAL IS {1. identifier-3
2. literal-2}
{1. INDICATORS
{1. IS
identifier-4
2. INDICATOR 2. ARE}
3. INDIC}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-REWRITE.
Example:
REWRITE SUBFILE EMPREC
FORMAT IS EMPSFL
INDICATORS ARE INDIC-AREA
INVALID KEY DISPLAY ERROR IN RRN
END-REWRITE.

ACQUIRE : is used to acquire a device for the specified transaction file.


On successful execution of the ACQUIRE statement the device is ready for further I/O
operations.
Syntax:
ACQUIRE {1. identifier FOR
2. literal}
DROP :

file-name

is used to release a device which has been acquired by the ACQUIRE statement.

Syntax:
DROP {1. identifier
2. literal}

FROM

file-name

LIKE : Clause is used with an OCCURS clause to define an element of the


Array as having the same attributes as a field outside the array .
WORKING-STORAGE SECTION.

12

05 AMOUNT-IN
PIC 9(3)V99.
01 MONTHLY-TOTAL-ARRAY.
05 MONTH-TOTAL
OCCURS 12 TIMES LIKE AMOUNT-IN(+2)

File Status Codes :


10

- End of file / Record not found

41 opening a file which was already open


42

- closing a file which was already closed

39

- File attributes mismatch

(Record length mismatch between COBOL and JCL )


22 Duplicate record insertion
23 Record not found
37 operation tried on a file with not opened in INPUT mode
46 when READ is executed on a file with no next record found.
47 READ has been tried on a file with not opened in INPUT
mode
92 Logical error
93 Resource not available / Authorization failure
9D Record is Locked
90 - File not found

Vous aimerez peut-être aussi