# Error Handling

> [← Back to README](../README.md) | [Decision Logic](./decision-logic.md) | [Business Logic](./business-logic.md)

## CICS RESP/RESP2 Error Handling

All online programs capture RESP and RESP2 codes on CICS commands:
```cobol
EXEC CICS READ FILE('filename')
    INTO(record-area) RIDFLD(key-field)
    RESP(WS-RESP-CD) RESP2(WS-REAS-CD)
```

### Common RESP Code Handling

| RESP Code | Constant | Meaning | Handling |
|----------:|:---------|:--------|:---------|
| 0 | DFHRESP(NORMAL) | Success | Continue processing |
| 12 | DFHRESP(FILENOTFOUND) | File not found | Display error message |
| 13 | DFHRESP(NOTFND) | Record not found | Display "not found" message |
| 14 | DFHRESP(DUPREC) | Duplicate record | Display "already exists" |
| 16 | DFHRESP(INVREQ) | Invalid request | Display error, re-display screen |
| 18 | DFHRESP(NOSPACE) | No space | Display "file full" error |

### Program-Specific Error Handling

**COSGN00C (lines 117–130, 221–257):**
- NOTFND → "User ID not found"
- NORMAL + wrong password → "Invalid password"
- OTHER → "System error occurred"

**COUSR01C (Add User, line 240):**
- DUPREC → "User already exists"
- NORMAL → "User added successfully"

**COUSR02C/03C (Update/Delete User):**
- NOTFND → "User not found"
- Uses READ with UPDATE lock, then REWRITE or DELETE

## ABEND Handling

Several programs register ABEND handlers via `EXEC CICS HANDLE ABEND`:

| Program | Line | ABEND Section |
|:--------|-----:|:-------------|
| COACTUPC | 862 | Abend handler section |
| COACTVWC | 264 | Abend handler section |
| COCRDSLC | 250 | Abend handler section |
| COCRDUPC | 370 | Abend handler section |

**Pattern:**
```cobol
EXEC CICS HANDLE ABEND SECTION(ABEND-ROUTINE)
...
ABEND-ROUTINE SECTION.
    MOVE EIBRESP TO WS-RESP-CD
    [Display error information on screen]
    EXEC CICS RETURN
```

## Batch Error Handling

### File Status Codes (Batch Programs)
Batch programs check FILE STATUS after each I/O:
```cobol
IF WS-FILE-STATUS NOT = '00'
    DISPLAY 'FILE ERROR: ' WS-FILE-STATUS
    STOP RUN
```

| Status | Meaning |
|:-------|:--------|
| '00' | Successful |
| '10' | End of file |
| '22' | Duplicate key |
| '23' | Record not found |
| '35' | File not found |

### CBTRN02C Error Handling (Transaction Posting)
- Invalid cross-reference → Transaction rejected, logged to reject file
- Account not found → Transaction rejected
- File I/O error → Job terminates with error status

### ABEND Work Areas (CSMSG02Y)
The CSMSG02Y copybook provides work areas for capturing and displaying ABEND information, including reason codes and descriptive messages.

## IMS DL/I Error Handling (Optional)

Programs check PCB status code after each DL/I call:
- 'GE' = Segment not found
- 'GB' = End of database
- 'II' = Segment already exists
- Blank = Success

## MQ Error Handling (Optional)

COPAUA0C, COACCT01, CODATE01 check MQ completion/reason codes:
- MQCC-OK (0) = Success
- MQCC-WARNING (1) = Warning
- MQCC-FAILED (2) = Failure
- MQRC-NO-MSG-AVAILABLE = No messages in queue

---

*See also: [Business Logic](./business-logic.md) | [Decision Logic](./decision-logic.md) | [Security Patterns](../analysis/security-patterns.md)*
