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.
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).
CICS response codes are numeric values that indicate the result of command execution. Understanding these codes is fundamental to effective error handling.
Code | Meaning | Description | Action Required |
---|---|---|---|
0 | Normal | Command completed successfully | Continue processing |
4 | Warning | Command completed with warnings | Check for potential issues |
8 | Error | Command failed | Handle error condition |
12 | Program Error | Program logic error | Debug and fix program |
13 | Not Found | Record or resource not found | Check search criteria |
20 | End of File | Reached end of file | Normal condition for sequential reads |
22 | Duplicate Key | Key already exists | Handle duplicate condition |
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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344IDENTIFICATION 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:
Beyond basic response code checking, advanced error handling techniques provide more sophisticated ways to manage errors and improve application reliability.
123456789101112131415161718192021222324252627282930WORKING-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.
123456789101112131415161718192021222324252627WORKING-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.
1234567891011121314151617181920212223242526PROCEDURE 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.
Following established best practices ensures that your CICS applications handle errors consistently and provide a better user experience.
Check EIBRESP after every CICS command, even if you expect success.
Give users clear, actionable information about what went wrong and how to proceed.
Log all errors with sufficient detail for debugging and monitoring purposes.
Implement proper commit/rollback mechanisms to maintain data consistency.
Mistake | Problem | Solution |
---|---|---|
Ignoring EIBRESP | Program continues with invalid data | Always check response codes |
Generic error handling | Poor user experience and debugging | Handle specific error types |
No error logging | Difficult to troubleshoot issues | Implement comprehensive logging |
Missing rollback | Data inconsistency | Use SYNCPOINT ROLLBACK |