Microsoft KB Archive/43745

From BetaArchive Wiki

How to Use COBOL DECLARATIVES Procedure for Error Handling

PSS ID Number: Q43745 Article last modified on 04-27-1989

2.00 2.10 2.20 3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The DECLARATIVES section is code that is executed not as part of the sequential coding written by the programmer, but when a condition occurs that is not tested by the programmer. If an I/O error occurs that is pertinent to the AT END or INVALID KEY condition and an AT END or INVALID KEY phrase does not exist to handle the error, then the imperative statements in the phrase are executed. When an error is detected with a file, control is transferred to the DECLARATIVES statement. The appropriate paragraph in the DECLARATIVES statement is then executed and control is returned to the line of code following where the error occurred.

More Information: Below is a code example of how the DECLARATIVES statement works when it is executed. The code will work in COBOL Version 2.20, and in COBOL Versions 3.00 and 3.00a with the ANS85 or ANS74 directive. For more information, please refer to Page 183 of the “Microsoft COBOL Reference Manual” for Version 2.20, and Page 9-6 of the “Microsoft COBOL Compiler Version 3.00: Language Reference Manual.”

Code Example: The code asks for 7 entries to an Indexed file, whose access is DYNAMIC. The program then tries to read back 8 entries, which causes a read past EOF. This error causes the code in the DECLARATIVES section to be executed, instead of aborting the program. IDENTIFICATION DIVISION. PROGRAM-ID. TESTus. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO DISK ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS PRIME-KEY FILE STATUS IS FILE-STAT. DATA DIVISION. FILE SECTION. FD INPUT-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS “TESTIN.DAT”. 01 INPUT-REC. 03 PRIME-KEY PIC 999. 03 REST-REC PIC XXX. WORKING-STORAGE SECTION. 77 FILE-STAT PIC XX. 01 SWITCHES. 03 ACCUM pic 9 value 1. 03 IN-NUMBERS pic 999 value zero. 03 IN-LETTERS pic xxx value spaces. 03 JUNK pic x value space. SCREEN SECTION. 01 GET-SCREEN. 05 BLANK SCREEN. 05 LINE 01 COLUMN 01 VALUE “enter a three digit number”. 05 LINE 02 COLUMN 02 PIC 9(3) TO IN-NUMBERS . 05 LINE 03 COLUMN 03 VALUE “enter three letters” . 05 LINE 04 COLUMN 04 PIC X(3) TO IN-LETTERS. 01 DISPLAY-SCREEN. 05 BLANK SCREEN. 05 LINE 01 COLUMN 01 PIC 9(3) FROM PRIME-KEY. 05 LINE 02 COLUMN 02 PIC X(3) FROM REST-REC. PROCEDURE DIVISION. ******************* * This is the declaratives section and it must be the first * section after the PROCEDURE DIVISION heading. * The first sentence identifies the section name and the * file name, identifier name, or procedure(s) that the section * applies to. * The second sentence is the code that is executed when an * error occurs that is within the section specified above. ******************** DECLARATIVES. input-file-error section. use after error procedure on INPUT-FILE. print-out-error. display “error occurred”. display FILE-STAT. accept JUNK. END DECLARATIVES. 000-MAIN. OPEN OUTPUT INPUT-FILE. PERFORM GET-INFO 7 TIMES. CLOSE INPUT-FILE. OPEN INPUT INPUT-FILE. MOVE 1 TO ACCUM. PERFORM READ-FILE 8 TIMES. CLOSE INPUT-FILE. STOP RUN. GET-INFO. DISPLAY GET-SCREEN. ACCEPT GET-SCREEN. MOVE IN-NUMBERS TO PRIME-KEY. MOVE IN-LETTERS TO REST-REC. WRITE INPUT-REC. ADD 1 TO ACCUM. READ-FILE. READ INPUT-FILE NEXT RECORD. DISPLAY DISPLAY-SCREEN. ACCEPT JUNK.

Copyright Microsoft Corporation 1989.