Condition management is a fundamental aspect of CICS programming that allows you to handle exception conditions gracefully. When CICS commands encounter unusual situations, they generate exception conditions. Understanding how to manage these conditions is essential for building robust, production-ready CICS applications.
Think of condition management like having a safety net for your program. Just as a trapeze artist has a net to catch them if they fall, condition management catches errors and allows your program to handle them gracefully instead of crashing unexpectedly.
Condition management in CICS refers to the mechanisms available to handle exception conditions that occur during the execution of CICS commands. An exception condition is an abnormal situation that arises when a CICS command encounters an unexpected or error state.
Without proper condition management, when an exception occurs, CICS typically terminates the task abnormally, resulting in an incomplete transaction and potential data inconsistency. Condition management allows you to:
CICS exception conditions can be categorized into several types:
The HANDLE CONDITION command is used to specify a label or paragraph that receives control when a specific exception condition occurs. This allows you to implement custom error handling logic for anticipated conditions.
123456EXEC CICS HANDLE CONDITION condition-name(label-name) [condition-name2(label-name2)] ... [ERROR(general-error-label)] END-EXEC.
Parameters:
12345678910111213141516171819202122232425262728293031323334353637383940IDENTIFICATION DIVISION. PROGRAM-ID. HANDLE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUST-ID PIC X(8). 05 CUST-NAME PIC X(30). 05 CUST-ADDRESS PIC X(50). PROCEDURE DIVISION. *> Set up condition handlers before commands that might fail EXEC CICS HANDLE CONDITION NOTFND(RECORD-NOT-FOUND) DUPREC(DUPLICATE-RECORD) ERROR(GENERAL-ERROR) END-EXEC. *> Attempt to read a customer record EXEC CICS READ DATASET('CUSTFILE') INTO(CUSTOMER-RECORD) RIDFLD(CUST-ID) END-EXEC. *> If we reach here, the read was successful DISPLAY 'Customer record read successfully' EXEC CICS RETURN END-EXEC. RECORD-NOT-FOUND. DISPLAY 'ERROR: Customer record not found' EXEC CICS RETURN END-EXEC. DUPLICATE-RECORD. DISPLAY 'ERROR: Duplicate record detected' EXEC CICS RETURN END-EXEC. GENERAL-ERROR. DISPLAY 'ERROR: An unexpected error occurred' EXEC CICS RETURN END-EXEC.
In this example, if the READ command cannot find the record, control transfers to RECORD-NOT-FOUND. If a duplicate record is detected during a WRITE operation, control goes to DUPLICATE-RECORD. Any other unhandled error goes to GENERAL-ERROR.
You can handle multiple conditions in a single HANDLE CONDITION command (up to 16 conditions):
12345678EXEC CICS HANDLE CONDITION MAPFAIL(MAP-ERROR-HANDLER) LENGERR(LENGTH-ERROR-HANDLER) PGMIDERR(PROGRAM-ERROR-HANDLER) NOTFND(FILE-ERROR-HANDLER) DUPREC(DUPLICATE-HANDLER) ERROR(GENERAL-ERROR-HANDLER) END-EXEC.
HANDLE CONDITION remains active from where it appears until:
Important: HANDLE CONDITION applies only to the program where it's specified. It does not automatically apply to programs called via LINK, XCTL, or CALL.
The RESP option is the modern, recommended approach for condition management. Instead of transferring control to a label, RESP sets a response code that you check after command execution. This provides more control and doesn't interrupt the normal program flow.
12345678910EXEC CICS command-name [other-options] RESP(response-variable) END-EXEC. IF response-variable = 0 THEN *> Command succeeded ELSE *> Handle error based on response code END-IF.
123456789101112131415161718192021222324252627282930313233343536373839404142IDENTIFICATION DIVISION. PROGRAM-ID. RESP-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUST-ID PIC X(8). 05 CUST-NAME PIC X(30). 01 WS-RESP PIC S9(8) COMP. 01 WS-RESP2 PIC S9(8) COMP. PROCEDURE DIVISION. *> Read customer record with RESP option EXEC CICS READ DATASET('CUSTFILE') INTO(CUSTOMER-RECORD) RIDFLD(CUST-ID) RESP(WS-RESP) RESP2(WS-RESP2) END-EXEC. *> Check response code EVALUATE WS-RESP WHEN 0 DISPLAY 'Record read successfully' *> Continue normal processing WHEN 13 *> NOTFND condition DISPLAY 'ERROR: Customer record not found' *> Handle not found condition WHEN 22 *> DUPREC condition DISPLAY 'ERROR: Duplicate record' *> Handle duplicate condition WHEN OTHER DISPLAY 'ERROR: Unexpected error - Response: ' WS-RESP *> Handle other errors END-EVALUATE. EXEC CICS RETURN END-EXEC.
Response Code Values:
The IGNORE CONDITION command tells CICS to take no action when a specific condition occurs. Execution continues to the next statement after the command that encountered the condition.
12345EXEC CICS IGNORE CONDITION condition-name [condition-name2] ... END-EXEC.
Use IGNORE CONDITION when you've determined that a specific condition is safe to ignore in your context. Common scenarios include:
1234567891011121314151617181920212223242526IDENTIFICATION DIVISION. PROGRAM-ID. IGNORE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 MAP-DATA PIC X(100). PROCEDURE DIVISION. *> Ignore LENGERR for this RECEIVE command *> We'll handle length checking manually EXEC CICS IGNORE CONDITION LENGERR END-EXEC. EXEC CICS RECEIVE MAP('CUSTMAP') INTO(MAP-DATA) RESP(WS-RESP) END-EXEC. *> If LENGERR occurs, it's ignored and we continue here *> We can check WS-RESP to see if there were other errors IF WS-RESP = 0 THEN DISPLAY 'Map received successfully' END-IF. EXEC CICS RETURN END-EXEC.
Important: Be careful when using IGNORE CONDITION. Only ignore conditions that you've thoroughly analyzed and determined are safe to ignore. Ignoring critical conditions can lead to data corruption or unexpected program behavior.
The NOHANDLE option can be specified on any CICS command to temporarily deactivate HANDLE CONDITION and IGNORE CONDITION for that specific command. This allows you to use default CICS behavior for a particular command while maintaining condition handling for other commands.
1234EXEC CICS command-name [other-options] NOHANDLE END-EXEC.
123456789101112131415161718192021222324252627282930313233343536373839IDENTIFICATION DIVISION. PROGRAM-ID. NOHANDLE-EXAMPLE. PROCEDURE DIVISION. *> Set up condition handlers EXEC CICS HANDLE CONDITION NOTFND(ERROR-HANDLER) ERROR(GENERAL-ERROR) END-EXEC. *> This command uses the HANDLE CONDITION EXEC CICS READ DATASET('FILE1') INTO(WS-RECORD1) RIDFLD(WS-KEY1) END-EXEC. *> This command ignores HANDLE CONDITION and uses default behavior EXEC CICS READ DATASET('FILE2') INTO(WS-RECORD2) RIDFLD(WS-KEY2) NOHANDLE END-EXEC. *> This command again uses the HANDLE CONDITION EXEC CICS READ DATASET('FILE3') INTO(WS-RECORD3) RIDFLD(WS-KEY3) END-EXEC. ERROR-HANDLER. DISPLAY 'Error handler called' EXEC CICS RETURN END-EXEC. GENERAL-ERROR. DISPLAY 'General error handler called' EXEC CICS RETURN END-EXEC.
In this example, the second READ command uses NOHANDLE, so if NOTFND occurs, it won't transfer to ERROR-HANDLER. Instead, CICS takes the default action (typically task termination). The first and third READ commands still use the HANDLE CONDITION.
Understanding common exception conditions helps you write better error handling code. Here are the most frequently encountered conditions:
| Condition | Description | Common Commands |
|---|---|---|
| NOTFND | Record not found in a file or dataset | READ, READNEXT, READPREV |
| DUPREC | Duplicate record detected (duplicate key) | WRITE |
| MAPFAIL | Map receive failed (no data received or invalid map) | RECEIVE MAP |
| LENGERR | Length error (data length mismatch) | RECEIVE, SEND |
| PGMIDERR | Program ID error (program not found or invalid) | LINK, XCTL, LOAD |
| INVREQ | Invalid request (invalid command parameters) | All commands |
| ILLOGIC | Illogical request (logical error in VSAM) | VSAM file operations |
| ITEMERR | Item error (item not found in queue) | READQ TS, READQ TD |
| QIDERR | Queue ID error (invalid queue identifier) | READQ TS, WRITEQ TS, DELETEQ TS |
| ENDDATA | End of data (end of file or queue) | READNEXT, READPREV, READQ |
When using the RESP option, each condition has a corresponding numeric response code:
Prefer the RESP option over HANDLE CONDITION for new CICS applications. It provides better control and maintains program flow.
After every CICS command that uses RESP, check the response code. Don't assume commands always succeed.
123456789101112EXEC CICS READ DATASET('CUSTFILE') INTO(WS-RECORD) RIDFLD(WS-KEY) RESP(WS-RESP) END-EXEC. *> ALWAYS check the response IF WS-RESP NOT = 0 THEN *> Handle error appropriately PERFORM ERROR-HANDLING END-IF.
When handling errors, provide clear, user-friendly error messages that help users understand what went wrong and what they can do about it.
Log error conditions with sufficient detail (response codes, context, timestamps) to help with debugging and troubleshooting in production.
Always set up HANDLE CONDITION commands before the CICS commands that might trigger those conditions. HANDLE CONDITION must be executed before the condition occurs.
Include an ERROR condition handler as a catch-all for unexpected conditions. This prevents unhandled errors from causing unexpected program termination.
Only ignore conditions that you've thoroughly analyzed. Ignoring critical conditions can lead to data corruption or security issues.
Don't just test the happy path. Test error conditions to ensure your error handling works correctly. Use test data that triggers various error conditions.
Here's a complete example showing proper condition management in a file processing scenario:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273IDENTIFICATION DIVISION. PROGRAM-ID. FILE-PROCESSING-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. 05 CUST-ID PIC X(8). 05 CUST-NAME PIC X(30). 05 CUST-ADDRESS PIC X(50). 05 CUST-BALANCE PIC S9(9)V99 COMP-3. 01 WS-RESP PIC S9(8) COMP. 01 WS-RESP2 PIC S9(8) COMP. 01 WS-ERROR-MSG PIC X(80). PROCEDURE DIVISION. MAIN-PROCESSING. *> Set up error handlers using HANDLE CONDITION EXEC CICS HANDLE CONDITION ERROR(GENERAL-ERROR-HANDLER) END-EXEC. *> Read customer record with RESP option EXEC CICS READ DATASET('CUSTFILE') INTO(CUSTOMER-RECORD) RIDFLD(CUST-ID) RESP(WS-RESP) RESP2(WS-RESP2) END-EXEC. *> Check response and handle accordingly EVALUATE WS-RESP WHEN 0 *> Success - process the record DISPLAY 'Customer: ' CUST-NAME DISPLAY 'Balance: ' CUST-BALANCE PERFORM PROCESS-CUSTOMER WHEN 13 *> NOTFND MOVE 'Customer record not found' TO WS-ERROR-MSG PERFORM DISPLAY-ERROR WHEN 22 *> DUPREC (shouldn't happen on READ, but handle it) MOVE 'Duplicate record error' TO WS-ERROR-MSG PERFORM DISPLAY-ERROR WHEN OTHER MOVE 'Unexpected error occurred' TO WS-ERROR-MSG PERFORM DISPLAY-ERROR *> Log the actual response code for debugging DISPLAY 'Response code: ' WS-RESP END-EVALUATE. EXEC CICS RETURN END-EXEC. PROCESS-CUSTOMER. *> Business logic to process customer record *> This is where you'd implement your actual processing logic DISPLAY 'Processing customer record...' . DISPLAY-ERROR. *> Display error message to user DISPLAY 'ERROR: ' WS-ERROR-MSG *> In a real application, you might send this to a map *> or log it to a file . GENERAL-ERROR-HANDLER. *> Catch-all error handler for unhandled conditions DISPLAY 'A general error occurred' EXEC CICS RETURN END-EXEC.
This example demonstrates:
1. What is the primary purpose of HANDLE CONDITION in CICS?
2. Which approach is recommended for modern CICS programming: HANDLE CONDITION or RESP option?
3. What happens when IGNORE CONDITION is used for a specific condition?
4. How many conditions can be specified in a single HANDLE CONDITION command?
5. What is the scope of a HANDLE CONDITION command?
6. What does the ERROR condition in HANDLE CONDITION do?
Learn about comprehensive error handling strategies in CICS applications
Understanding exception handling mechanisms and best practices
Learn about CICS file operations including READ, WRITE, and DELETE commands
Understanding program control commands and error handling in program calls