MainframeMaster

CICS Error Handling

Progress0 of 0 lessons

Error handling is a critical aspect of CICS application development. Understanding how to properly handle errors, interpret response codes, and implement robust exception handling mechanisms is essential for building reliable mainframe applications.

What is CICS Error Handling?

CICS error handling is the systematic approach to detecting, analyzing, and responding to errors that occur during program execution. Unlike modern programming languages with try-catch blocks, CICS uses response codes and conditional logic to manage error conditions.

Think of CICS error handling like a traffic light system. Each CICS command returns a response code (like a traffic light color), and your program must check this code and take appropriate action (like stopping, proceeding with caution, or taking an alternate route).

Key Components of CICS Error Handling:

  • EIBRESP: Primary response code indicating success or failure
  • EIBRESP2: Secondary response code with additional error details
  • Exception Handling: Program logic to manage error conditions
  • Error Recovery: Mechanisms to restore normal operation
  • User Communication: Informing users about errors and next steps

Response Codes and Their Meanings

CICS response codes are numeric values that indicate the result of command execution. Understanding these codes is fundamental to effective error handling.

Common Response Codes:

CodeMeaningDescriptionAction Required
0NormalCommand completed successfullyContinue processing
4WarningCommand completed with warningsCheck for potential issues
8ErrorCommand failedHandle error condition
12Program ErrorProgram logic errorDebug and fix program
13Not FoundRecord or resource not foundCheck search criteria
20End of FileReached end of fileNormal condition for sequential reads
22Duplicate KeyKey already existsHandle duplicate condition

Basic Error Handling in COBOL

In COBOL, error handling is implemented using conditional statements that check the EIBRESP field after each CICS command. This approach ensures that errors are caught and handled appropriately.

Standard Error Handling Pattern:

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
IDENTIFICATION DIVISION. PROGRAM-ID. ERROR-HANDLING-EXAMPLE. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUST-ID PIC X(10). 05 CUST-NAME PIC X(30). 05 CUST-BALANCE PIC 9(7)V99. 01 EIBRESP PIC S9(8) COMP. 01 EIBRESP2 PIC S9(8) COMP. PROCEDURE DIVISION. MAIN-LOGIC. PERFORM READ-CUSTOMER PERFORM PROCESS-CUSTOMER PERFORM WRITE-CUSTOMER STOP RUN. READ-CUSTOMER. EXEC CICS READ DATASET('CUSTFILE') INTO(CUSTOMER-RECORD) RIDFLD(CUST-ID) RESP(EIBRESP) RESP2(EIBRESP2) END-EXEC IF EIBRESP NOT = 0 PERFORM HANDLE-READ-ERROR END-IF. HANDLE-READ-ERROR. EVALUATE EIBRESP WHEN 13 DISPLAY 'Customer not found: ' CUST-ID WHEN 20 DISPLAY 'End of file reached' WHEN OTHER DISPLAY 'Read error: ' EIBRESP END-EVALUATE.

This example demonstrates the fundamental pattern of CICS error handling in COBOL:

  • Check EIBRESP: After each CICS command, examine the response code
  • Conditional Logic: Use IF statements or EVALUATE to handle different error conditions
  • Specific Handling: Provide different responses for different error types
  • Error Procedures: Separate procedures for handling specific error conditions

Advanced Error Handling Techniques

Beyond basic response code checking, advanced error handling techniques provide more sophisticated ways to manage errors and improve application reliability.

1. Error Logging and Recovery:

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
WORKING-STORAGE SECTION. 01 ERROR-LOG. 05 ERROR-TIME PIC X(8). 05 ERROR-COMMAND PIC X(16). 05 ERROR-RESP PIC S9(8) COMP. 05 ERROR-RESP2 PIC S9(8) COMP. 05 ERROR-MESSAGE PIC X(80). PROCEDURE DIVISION. LOG-ERROR. MOVE FUNCTION CURRENT-DATE TO ERROR-TIME MOVE 'READ' TO ERROR-COMMAND MOVE EIBRESP TO ERROR-RESP MOVE EIBRESP2 TO ERROR-RESP2 EXEC CICS WRITE DATASET('ERRORLOG') FROM(ERROR-LOG) RESP(EIBRESP) END-EXEC. RECOVERY-ATTEMPT. IF EIBRESP = 13 PERFORM RETRY-WITH-ALTERNATE-KEY ELSE IF EIBRESP = 22 PERFORM GENERATE-NEW-KEY ELSE PERFORM ABEND-PROCESSING END-IF.

2. User-Friendly Error Messages:

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
WORKING-STORAGE SECTION. 01 USER-MESSAGE PIC X(79). 01 ERROR-DETAILS PIC X(79). PROCEDURE DIVISION. DISPLAY-USER-ERROR. EVALUATE EIBRESP WHEN 13 MOVE 'Customer not found. Please check the ID and try again.' TO USER-MESSAGE WHEN 22 MOVE 'A customer with this ID already exists. Please use a different ID.' TO USER-MESSAGE WHEN 8 MOVE 'System temporarily unavailable. Please try again later.' TO USER-MESSAGE WHEN OTHER MOVE 'An unexpected error occurred. Please contact support.' TO USER-MESSAGE END-EVALUATE EXEC CICS SEND FROM(USER-MESSAGE) LENGTH(79) ERASE END-EXEC.

3. Transaction Rollback and Recovery:

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
PROCEDURE DIVISION. PROCESS-TRANSACTION. PERFORM UPDATE-CUSTOMER IF EIBRESP NOT = 0 PERFORM ROLLBACK-TRANSACTION PERFORM NOTIFY-USER-OF-FAILURE ELSE PERFORM COMMIT-TRANSACTION PERFORM NOTIFY-USER-OF-SUCCESS END-IF. ROLLBACK-TRANSACTION. EXEC CICS SYNCPOINT ROLLBACK RESP(EIBRESP) END-EXEC IF EIBRESP NOT = 0 PERFORM LOG-ROLLBACK-FAILURE END-IF. COMMIT-TRANSACTION. EXEC CICS SYNCPOINT RESP(EIBRESP) END-EXEC.

Best Practices for Error Handling

Following established best practices ensures that your CICS applications handle errors consistently and provide a better user experience.

Error Handling Best Practices:

Always Check Response Codes

Check EIBRESP after every CICS command, even if you expect success.

Provide Meaningful Error Messages

Give users clear, actionable information about what went wrong and how to proceed.

Implement Proper Logging

Log all errors with sufficient detail for debugging and monitoring purposes.

Use Transaction Management

Implement proper commit/rollback mechanisms to maintain data consistency.

Common Mistakes to Avoid:

MistakeProblemSolution
Ignoring EIBRESPProgram continues with invalid dataAlways check response codes
Generic error handlingPoor user experience and debuggingHandle specific error types
No error loggingDifficult to troubleshoot issuesImplement comprehensive logging
Missing rollbackData inconsistencyUse SYNCPOINT ROLLBACK

Quick Quiz

Question 1: What does EIBRESP = 0 indicate?

Question 2: Which response code indicates "not found"?

Question 3: What is the purpose of EIBRESP2?