USE Statement COBOL

The USE statement specifies procedures for input-output error handling beyond the standard procedures provided by the runtime system. It is a compiler directing statement required in each declarative section.

USE Statement

A USE statement must immediately follow a section header in the declaratives portion of the Procedure Division and must be followed by a separator period. The remainder of the section must consist of zero or more paragraphs that define the procedures to be used.

The USE statement itself is not executed; it defines the conditions calling for the execution of the USE procedure.

A file-name may not be listed in more than one USE statement, nor may it appear more than once in the list of any USE statement. File-names that appear in a USE statement list may not be SORT or MERGE files.

The appearance of a file-name in a USE statement must not cause the simultaneous request for execution of more than one USE procedure.

The INPUT, OUTPUT, I-O and EXTEND phrases may each be specified only once in the declaratives portion of a given Procedure Division.

The words ERROR and EXCEPTION are synonymous in this context.

Declarative procedures may be included in any source program irrespective of whether the program contains or is contained within another program. A declarative is invoked when any of the conditions described in the USE statement that prefaces the declarative occurs while the program is being executed. Only a declarative within the separately compiled program that contains the statement which caused the qualifying condition is invoked when any of the conditions described in the USE statement which prefaces the declarative occurs while that separately compiled program is being executed. If no qualifying declarative exists in the separately compiled program, no declarative is executed.

During the execution of an input-output statement, the runtime system executes the section associated with a USE statement under these conditions:

  • An invalid key condition occurs and there is no INVALID KEY phrase in the input-output statement.
  • An at end condition occurs and there is no AT END phrase in the input-output statement.
  • Some other exception or error condition arises.

The USE section is executed as if it were the operand of a Format 1 PERFORM statement, after having stored the I-O status value into the associated file status data item if there is one.

In circumstances where it is appropriate to do so, the system standard input-output error recovery procedures are also performed.

The rules that determine which USE procedure is to be executed are as follows:

  1. If file-name-1 is specified in the USE statement, the associated procedure is executed when the situation defined above arises during the execution of an input-output statement that refers to file-name-1.
  2. If the INPUT phrase is specified in the USE statement, the associated procedure is executed when the situation defined above arises during the execution of an input-output statement that refers to any file that is open in the input mode or is in the process of being opened in the input mode, provided the file is not referenced explicitly by name in another USE statement.
  3. If the OUTPUT phrase is specified in the USE statement, the associated procedure is executed when the situation defined above arises during the execution of an input-output statement that refers to any file that is open in the output mode or is in the process of being opened in the output mode, provided the file is not referenced explicitly by name in another USE statement.
  4. If the I-O phrase is specified in the USE statement, the associated procedure is executed when the situation defined above arises during the execution of an input-output statement that refers to any file that is open in the I-O mode or is in the process of being opened in the I-O mode, provided the file is not referenced explicitly by name in another USE statement.
  5. If the EXTEND phrase is specified in the USE statement, the associated procedure is executed when the situation defined above arises during the execution of an input-output statement that refers to any file that is open in the extend mode or is in the process of being opened in the extend mode, provided the file is not referenced explicitly by name in another USE statement.

When the execution of the USE procedure is complete, control returns to the runtime system. The runtime system then resumes execution of the COBOL program at the next executable statement following the input-output statement whose execution caused the exception or error.

When there is no applicable USE procedure and a critical error occurs for an input-output statement, the runtime system produces an error message and terminates execution of the run unit. This behavior can be configured to allow the program to continue as if a default empty USE procedure were applicable. See the DEFAULT-USE-PROCEDURE keyword of the COMPILER-OPTIONS record in Chapter Configuration, of the RM/COBOL User’s Guide for, information on configuring this behavior.

Within a USE procedure there must be no reference to any nondeclarative procedure. Conversely, in the nondeclarative portion there must be no reference to procedure-names that appear in the declarative portion, except that PERFORM statements may refer to the procedures associated with a USE statement.

Within a USE procedure, there must not be the execution of any statement that would cause the execution of a USE procedure that had previously been invoked and had not yet returned control to the invoking routine.

Special precedence rules are followed when programs are contained within other programs. In applying these rules, only the first qualifying declarative will be selected for execution. The declarative that is selected for execution must satisfy the rules for execution of that declarative. The order of precedence for selecting a declarative is:

  1. The declarative within the program that contains the statement which caused the qualifying condition.
  2. The declarative in which the GLOBAL phrase is specified and which is within the program directly containing the program that was last examined for a qualifying declarative.
  3. Any declarative selected by applying rule 2 to each more inclusive containing program until rule 2 is applied to the outermost program. If no qualifying declarative is found, none is executed.

USE Statement Example

PROCEDURE DIVISION.
DECLARATIVES.
I-O-ERROR SECTION.
USE AFTER STANDARD EXCEPTION PROCEDURE ON I-O.
I-O-ERROR-ROUTINE.
DISPLAY "Error for file in I-O open mode.".
ACCEPT CONTINUE-FLAG POSITION 0 PROMPT.
IF CONTINUE-FLAG = "NO" STOP RUN.
END DECLARATIVES.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics