Coding ERROR declaratives

You can code one or more ERROR declarative procedures that will be given control if an input or output error occurs during the execution of your program. If you do not code such procedures, your job could be canceled or abnormally terminated after an input or output error occurs.

About this task

Place each such procedure in the declaratives section of the PROCEDURE DIVISION. You can code:

  • A single, common procedure for the entire program
  • Procedures for each file open mode (whether INPUT, OUTPUT, I-O, or EXTEND)
  • Individual procedures for each file

In an ERROR declarative procedure, you can code corrective action, retry the operation, continue, or end execution. (If you continue processing a blocked file, though, you might lose the remaining records in a block after the record that caused the error.) You can use the ERROR declaratives procedure in combination with the file status key if you want a further analysis of the error.

Multithreading: Avoid deadlocks when coding I/O declaratives in multithreaded applications. When an I/O operation results in a transfer of control to an I/O declarative, the automatic serialization lock associated with the file is held during the execution of the statements within the declarative. If you code I/O operations within your declaratives, your logic might result in a deadlock as illustrated by the following sample:



Declaratives.
D1 section.
Use after standard error procedure on F1
    Read F2.
    . . .
D2 section.
Use after standard error procedure on F2
    Read F1.
    . . .
End declaratives.
    . . .
    Rewrite R1.
    Rewrite R2.

When this program is running on two threads, the following sequence of events could occur:

Procedure

  1. Thread 1: Rewrite R1 acquires lock on F1 and encounters I/O error.
  2. Thread 1: Enter declarative D1, holding lock on F1.
  3. Thread 2: Rewrite R2 acquires lock on F2 and encounters I/O error.
  4. Thread 2: Enter declarative D2.
  5. Thread 1: Read F2 from declarative D1; wait on F2 lock held by thread 2.
  6. Thread 2: Read F1 from declarative D2; wait on F1 lock held by thread 1.
  7. Deadlock.

Results

Related references  
EXCEPTION/ERROR declarative (Enterprise COBOL for z/OS® Language Reference)