cobol new final

15
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

Upload: saiprasad

Post on 14-Dec-2015

215 views

Category:

Documents


3 download

DESCRIPTION

hi

TRANSCRIPT

Page 1: Cobol New Final

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) PROCEDUREON 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-STATUSWORKING-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.

Page 2: Cobol New Final

2

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 didn’t 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 opened43 - 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 .

Page 3: Cobol New Final

3

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 DIVISIONINPUT-OUTPUT SECTION.FILE-CONTROL.

SELECT DISPLAY-FILE ASSIGN TO WORKSTATION-RBSFORGANIZATION IS TRANSACTIONACCESS MODE IS DYNAMICRELATIVE KEY IS SUBFILE-REL-NOFILE STATUS IS STATUS-RB

WORKING-STORAGE SECTION.01 ERRHDL-PARAM.

05 STATUS-RB PIC X(2).88 SUBFILE-IS-FULL VALUE ‘9M’

PROCEDURE DIVISION.DECLARATIVES.TRANSACTION-ERROR SECTION.

USE AFTER STANDARA ERROR PROCEDURE DISPLAY-FILEERROR-HANDLER.

IF SUBFILE-IS-FULL THENNEXT SENTENCEELSEDISPLAY ‘ WORK-STATION ERROR’ STATUS-DSP.

END-DECLARATIVES.

Page 4: Cobol New Final

4

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

COMMITELSE

ROLLBACKEND-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.

Page 5: Cobol New Final

5

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 job’s Local data area is copied into the submitted Job’s 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 areaDISPLAY WS-DATA-AREA UPON LOCAL-DATA-AREA.

SPECIAL-NAMES.LOCAL-DATA IS LOCAL-DATA-AREAREQUESTOR IS OPERATOR

WORKING-STORAGE SECTION.77 WK-LOCAL PIC X(1024)PROCEDURE DIVISION.

ACCEPT WK-LOCAL FROM LOCAL-DATA-AREA

Page 6: Cobol New Final

6

ENVIRONMENT DIVISION

FILE-CONTROL

SELECT temp-file ASSIGN TO device-type – file-name ORGANIZATION IS {1. INDEXED

2. SEQUENTIAL3. RELATIVE4. TRANSACTION}

ACCESS MODE IS { 1. SEQUENTIAL2. RANDOM3. DYNAMIC}

FILE STATUS IS identifier-1.

FILE SECTIONFD 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 mode file-name-2…

Ex : OPEN INPUT NEW-FILE.

2 ) CLOSE

CLOSE file-name-1, file-name-2 ….

3) READ

SEQUENTIAL RETRIEVAL USING SEQUENTIAL ACCESSSyntax:

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]

Page 7: Cobol New Final

7

Example: READ TEMP-FILE NEXT RECORD INTO RECORD-1

AT END MOVE B”1” 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 B”1” 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]

Example:

READ TEMP-FILEKEY IS STUID OF TEMP-FILEINVALID KEY DISPLAY “not present”NOT INVALID KEY DISPLAY “present”END-READ.

WRITE

SEQUENTIALSyntax:

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.

Page 8: Cobol New Final

8

Example:

WRITE NEWREC FROM TEMPRECAT 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-1FORMAT IS {1. identifier-2

2. literal-1}INVALID KEY imperative-statement-1NOT INVALID KEY imperative-statement-2END-WRITE.

Example:

WRITE NEW-REC FROM TEMP-RECFORMAT IS “ADDREC”INVALID KEY DISPLAY “error in write statement”END-WRITE.

RELATIVE FILES

Syntax:

WRITE record-name-1 FROM identifier-1FORMAT IS {1. identifier-2

2. literal-1}{1. INDICATORS {1. IS identifier-3 2. INDICATOR 2. ARE} 3. INDIC}AT {1. EOP imperative-statement-1 2. END-OF-PAGE}NOT AT {1. EOP imperative-statement-2

2. END-OF-PAGE}

Example:

WRITE NEW-RECFORMAT IS “ADDREC”INDICATORS ARE INDIC-AREA.

Page 9: Cobol New Final

9

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-1NOT INVALID KEY imperative-statement-2

Example:START EMPLOYEE KEY IS EQUAL TO

EXTERNALLY-DESCRIBED KEYINVALID KEY DISPLAY “Employee does not exist”NOT INVALID KEY DISPLAY “Employee found”

END-START.

REWRITE

Syntax:REWRITE record-name-1 FROM identifier-1FORMAT IS {1. identifier-2

2. literal-1}INVALID KEY imperative-statement-1NOT INVALID KEY imperative-statement-2END-REWRITE.

Example:REWRITE NEW-REC FORMAT IS “EDTREC”.

DELETE

Syntax:DELETE filename RECORDFORMAT IS identifier-1INVALID KEY imperative-statement-1NOT INVALID KEY imperative-statement-2END-DELETE.

Example:DELETE TEMP-FILEFORMAT IS TEMP-RECINVALID KEY DISPLAY “DELETION NOT DONE”NOT INVALID KEY DISPLAY “DELETED SUCCESSFUL”END-DELETE.

Page 10: Cobol New Final

10

SUBFILES

READSyntax:

READ SUBFILE filename NEXT MODIFIED RECORDINTO indentifier-1FORMAT 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-1NOT INVALID KEY imperative-statement-2AT END imperative-statement-3NOT AT END imperative-statement-4END-READ

Example:

READ SUBFILE EMPLOYEENEXT MODIFIED RECORDFORMAT IS “EMPSFL”INDICATORS ARE INDIC-AREAEND-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-1NOT INVALID KEY imperative-statement-2END-WRITE.

Example:

WRITE SUBFILE EMPREC FORMAT IS “EMPSFL”INDICATORS ARE INDIC-AREAINVALID KEY DISPLAY “ERROR IN RRN VALUE”END-WRITE.

Page 11: Cobol New Final

11

REWRITE

Syntax:

REWRITE SUBFILE record-name-1 FROM identifier-1FORMAT 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-1NOT INVALID KEY imperative-statement-2END-REWRITE.

Example:

REWRITE SUBFILE EMPRECFORMAT IS “EMPSFL”INDICATORS ARE INDIC-AREAINVALID 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 file-name 2. literal}

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

Syntax:DROP {1. identifier FROM file-name

2. literal}

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.

05 AMOUNT-IN PIC 9(3)V99. 01 MONTHLY-TOTAL-ARRAY. 05 MONTH-TOTAL

OCCURS 12 TIMES LIKE AMOUNT-IN(+2)

Page 12: Cobol New Final

12

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