MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Error Recovery

Error recovery in COBOL involves detecting, handling, and recovering from errors that occur during program execution. Effective error recovery ensures programs handle errors gracefully, provide meaningful error messages, attempt recovery when possible, and terminate cleanly when recovery isn't possible. Error recovery is essential for robust, production-quality COBOL programs that must operate reliably in enterprise environments.

This comprehensive guide will teach you how to handle file errors using FILE STATUS, detect arithmetic errors with ON SIZE ERROR, implement error recovery strategies, log errors effectively, and follow best practices for robust error handling. Whether you're handling file I/O errors, arithmetic exceptions, or data validation failures, mastering error recovery is crucial for building reliable COBOL applications.

What is Error Recovery?

Error recovery encompasses:

  • Error Detection: Identifying when errors occur
  • Error Handling: Responding to errors appropriately
  • Recovery Strategies: Attempting to recover from errors
  • Error Logging: Recording errors for diagnosis
  • Graceful Degradation: Continuing operation when possible
  • Clean Termination: Ending programs cleanly when recovery isn't possible

File Error Handling

File errors are handled by checking FILE STATUS after file operations. FILE STATUS is a two-character field that indicates the result of file operations.

Defining FILE STATUS

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "INPUT.DAT" ORGANIZATION IS SEQUENTIAL FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD PIC X(100). WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC X(2).

FILE STATUS is defined in FILE-CONTROL with FILE STATUS IS status-field-name. Check it after each file operation.

Checking FILE STATUS

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
PROCEDURE DIVISION. MAIN-LOGIC. OPEN INPUT INPUT-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "ERROR: File open failed. Status: " FILE-STATUS-CODE PERFORM HANDLE-FILE-ERROR STOP RUN END-IF PERFORM UNTIL FILE-STATUS-CODE = "10" READ INPUT-FILE AT END CONTINUE NOT AT END IF FILE-STATUS-CODE = "00" PERFORM PROCESS-RECORD ELSE DISPLAY "ERROR: Read failed. Status: " FILE-STATUS-CODE PERFORM HANDLE-READ-ERROR END-IF END-READ END-PERFORM CLOSE INPUT-FILE STOP RUN. HANDLE-FILE-ERROR. EVALUATE FILE-STATUS-CODE WHEN "30" DISPLAY "Permanent error - file cannot be accessed" WHEN "35" DISPLAY "File not found" WHEN OTHER DISPLAY "Unknown file error: " FILE-STATUS-CODE END-EVALUATE.

Always check FILE STATUS after file operations. Status "00" means success, other values indicate errors.

Common FILE STATUS Codes

Common FILE STATUS Codes
Status CodeMeaningTypical Action
"00"Successful completionContinue processing
"02"Duplicate key (indexed files)Handle duplicate or skip
"04"Record length mismatchCheck record structure
"10"End of fileNormal condition, stop reading
"22"Duplicate keyHandle duplicate key error
"23"Record not foundHandle missing record
"30"Permanent errorCannot recover, terminate
"35"File not foundCheck file existence

Arithmetic Error Handling

Arithmetic errors are handled using ON SIZE ERROR for overflow detection and explicit checks for division by zero.

ON SIZE ERROR

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WORKING-STORAGE SECTION. 01 AMOUNT-1 PIC 9(5) VALUE 99999. 01 AMOUNT-2 PIC 9(5) VALUE 1. 01 RESULT PIC 9(5). 01 ERROR-FLAG PIC X(1) VALUE 'N'. 88 ERROR-OCCURRED VALUE 'Y'. PROCEDURE DIVISION. MAIN-LOGIC. ADD AMOUNT-1 TO AMOUNT-2 GIVING RESULT ON SIZE ERROR DISPLAY "ERROR: Arithmetic overflow" SET ERROR-OCCURRED TO TRUE MOVE 99999 TO RESULT *> Set to maximum value END-ADD IF ERROR-OCCURRED DISPLAY "Result capped at maximum: " RESULT ELSE DISPLAY "Result: " RESULT END-IF STOP RUN.

ON SIZE ERROR detects when the result of an arithmetic operation is too large to fit in the target field. Handle overflow gracefully.

Division by Zero Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-STORAGE SECTION. 01 DIVIDEND PIC 9(5) VALUE 1000. 01 DIVISOR PIC 9(5) VALUE 0. 01 QUOTIENT PIC 9(5). PROCEDURE DIVISION. MAIN-LOGIC. IF DIVISOR = 0 DISPLAY "ERROR: Division by zero not allowed" MOVE 0 TO QUOTIENT ELSE DIVIDE DIVIDEND BY DIVISOR GIVING QUOTIENT ON SIZE ERROR DISPLAY "ERROR: Division result overflow" MOVE 0 TO QUOTIENT END-DIVIDE END-IF DISPLAY "Quotient: " QUOTIENT STOP RUN.

Always check for division by zero before DIVIDE operations. Handle the error appropriately.

Error Recovery Strategies

Common error recovery strategies:

  • Retry: Attempt the operation again after a delay or fix
  • Alternative Processing: Use backup methods or default values
  • Skip and Continue: Skip the problematic record and continue processing
  • Partial Recovery: Recover what's possible and report what couldn't be recovered
  • Graceful Degradation: Reduce functionality but continue operating
  • Clean Termination: Save state and terminate cleanly when recovery isn't possible

Retry Strategy Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
WORKING-STORAGE SECTION. 01 RETRY-COUNT PIC 9(2) VALUE 0. 01 MAX-RETRIES PIC 9(2) VALUE 3. 01 FILE-STATUS-CODE PIC X(2). PROCEDURE DIVISION. MAIN-LOGIC. PERFORM UNTIL FILE-STATUS-CODE = "00" OR RETRY-COUNT >= MAX-RETRIES ADD 1 TO RETRY-COUNT OPEN INPUT INPUT-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Open attempt " RETRY-COUNT " failed. Status: " FILE-STATUS-CODE IF RETRY-COUNT < MAX-RETRIES DISPLAY "Retrying in 1 second..." *> Wait before retry (implementation-specific) END-IF ELSE DISPLAY "File opened successfully on attempt " RETRY-COUNT END-IF END-PERFORM IF FILE-STATUS-CODE NOT = "00" DISPLAY "ERROR: Could not open file after " MAX-RETRIES " attempts" STOP RUN END-IF.

Skip and Continue Strategy

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
PROCEDURE DIVISION. MAIN-LOGIC. OPEN INPUT INPUT-FILE PERFORM UNTIL FILE-STATUS-CODE = "10" READ INPUT-FILE AT END CONTINUE NOT AT END IF FILE-STATUS-CODE = "00" PERFORM VALIDATE-RECORD IF RECORD-IS-VALID PERFORM PROCESS-RECORD ELSE ADD 1 TO INVALID-RECORD-COUNT DISPLAY "Skipping invalid record" END-IF ELSE ADD 1 TO ERROR-COUNT DISPLAY "Read error. Status: " FILE-STATUS-CODE DISPLAY "Skipping record and continuing" END-IF END-READ END-PERFORM CLOSE INPUT-FILE DISPLAY "Processing complete. Errors: " ERROR-COUNT " Invalid records: " INVALID-RECORD-COUNT STOP RUN.

Error Logging

Log errors with sufficient detail for diagnosis:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-STORAGE SECTION. 01 ERROR-LOG-RECORD. 05 ERROR-TYPE PIC X(20). 05 ERROR-CODE PIC X(10). 05 ERROR-MESSAGE PIC X(100). 05 ERROR-TIMESTAMP PIC X(14). 05 ERROR-CONTEXT PIC X(50). PROCEDURE DIVISION. LOG-ERROR. *> Get current date/time ACCEPT ERROR-TIMESTAMP FROM DATE YYYYMMDD ACCEPT ERROR-TIMESTAMP(9:6) FROM TIME *> Write to error log file WRITE ERROR-LOG-RECORD *> Also display for immediate visibility DISPLAY "ERROR [" ERROR-TIMESTAMP "]: " ERROR-TYPE " Code: " ERROR-CODE " " ERROR-MESSAGE.

Include in error logs: error type, error code, descriptive message, timestamp, and context information.

Complete Error Recovery Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
IDENTIFICATION DIVISION. PROGRAM-ID. ERROR-RECOVERY-EXAMPLE. AUTHOR. MainframeMaster Tutorial. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "INPUT.DAT" ORGANIZATION IS SEQUENTIAL FILE STATUS IS FILE-STATUS. SELECT ERROR-LOG ASSIGN TO "ERRORLOG.DAT" ORGANIZATION IS SEQUENTIAL FILE STATUS IS ERROR-LOG-STATUS. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD PIC X(100). FD ERROR-LOG. 01 ERROR-RECORD PIC X(200). WORKING-STORAGE SECTION. 01 FILE-STATUS PIC X(2). 01 ERROR-LOG-STATUS PIC X(2). 01 RECORD-COUNT PIC 9(6) VALUE 0. 01 ERROR-COUNT PIC 9(6) VALUE 0. 01 SUCCESS-COUNT PIC 9(6) VALUE 0. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Error Recovery Example ===" PERFORM OPEN-FILES IF FILE-STATUS = "00" PERFORM PROCESS-FILE PERFORM CLOSE-FILES ELSE DISPLAY "FATAL: Cannot open input file. Status: " FILE-STATUS END-IF DISPLAY "Processing complete:" DISPLAY " Records processed: " RECORD-COUNT DISPLAY " Successful: " SUCCESS-COUNT DISPLAY " Errors: " ERROR-COUNT STOP RUN. OPEN-FILES. OPEN INPUT INPUT-FILE IF FILE-STATUS NOT = "00" PERFORM LOG-ERROR EXIT PARAGRAPH END-IF OPEN OUTPUT ERROR-LOG IF ERROR-LOG-STATUS NOT = "00" DISPLAY "WARNING: Cannot open error log. Status: " ERROR-LOG-STATUS END-IF. PROCESS-FILE. PERFORM UNTIL FILE-STATUS = "10" READ INPUT-FILE AT END CONTINUE NOT AT END ADD 1 TO RECORD-COUNT IF FILE-STATUS = "00" PERFORM PROCESS-RECORD ELSE ADD 1 TO ERROR-COUNT PERFORM HANDLE-READ-ERROR END-IF END-READ END-PERFORM. PROCESS-RECORD. *> Process record with error handling *> Example: arithmetic operation *> (Implementation would include actual processing) ADD 1 TO SUCCESS-COUNT. HANDLE-READ-ERROR. DISPLAY "ERROR: Read failed. Status: " FILE-STATUS PERFORM LOG-ERROR *> Continue processing - skip and continue strategy. LOG-ERROR. *> Log error to file and display DISPLAY "Error logged: Status " FILE-STATUS *> (Implementation would write to error log file). CLOSE-FILES. CLOSE INPUT-FILE IF FILE-STATUS NOT = "00" DISPLAY "WARNING: Error closing input file. Status: " FILE-STATUS END-IF CLOSE ERROR-LOG IF ERROR-LOG-STATUS NOT = "00" DISPLAY "WARNING: Error closing error log. Status: " ERROR-LOG-STATUS END-IF.

This complete example demonstrates comprehensive error recovery with file status checking, error logging, and skip-and-continue strategy.

Best Practices for Error Recovery

  • Always check FILE STATUS: Check after every file operation
  • Use ON SIZE ERROR: Handle arithmetic overflow
  • Validate input: Prevent errors before they occur
  • Implement retry logic: For transient errors
  • Log all errors: With sufficient detail
  • Provide meaningful messages: Help users understand errors
  • Attempt recovery: When possible
  • Terminate cleanly: When recovery isn't possible
  • Save state: Before critical operations
  • Test error handling: With various error scenarios

Explain Like I'm 5: Error Recovery

Think of error recovery like fixing mistakes:

  • Detecting errors is like noticing something went wrong
  • Handling errors is like deciding what to do about it
  • Retrying is like trying again after fixing the problem
  • Skipping is like moving on when something can't be fixed
  • Logging errors is like writing down what went wrong so you can learn from it

So error recovery is like having a plan for when things go wrong - you notice the problem, try to fix it, and if you can't, you handle it gracefully!

Test Your Knowledge

1. What is FILE STATUS used for?

  • To check file size
  • To indicate the result of file operations
  • To set file permissions
  • To lock files

2. What FILE STATUS code indicates successful completion?

  • "01"
  • "00"
  • "10"
  • "99"

3. What is ON SIZE ERROR used for?

  • File errors
  • Arithmetic overflow detection
  • Input validation
  • Output formatting

4. What FILE STATUS code indicates end of file?

  • "00"
  • "10"
  • "20"
  • "30"

5. What should you do after a file operation?

  • Nothing, it always succeeds
  • Check FILE STATUS to detect errors
  • Close the file immediately
  • Display a message

6. What is a common error recovery strategy?

  • Ignore all errors
  • Retry the operation, use alternatives, or skip and continue
  • Always terminate the program
  • Never log errors

7. What should error logs include?

  • Only error codes
  • Error type, code, context, timestamp, and recovery actions
  • Only timestamps
  • Only file names

8. When should you use ON SIZE ERROR?

  • For all arithmetic operations
  • For arithmetic operations where overflow is possible
  • Only for division
  • Never use it

Related Concepts

Related Pages