COBOL Std 7 Error Handling
Jump to navigation
Jump to search
7. Error Handling[edit]
7.1 Return-Code Standard[edit]
All programs and DAL interfaces use the following result-code scheme via DAL-RESULT / SESSION-RESULT / UI-RESULT:
| Code | Meaning | Required Action |
|---|---|---|
0 |
Success | Continue processing. |
1 |
Error — recoverable | Log message; set result; return to caller for decision. |
2 |
End-of-data / not found | Handle gracefully; this is not an error condition. |
3 |
Warning — processing continued | Log warning; continue. |
8 |
Fatal error — terminate | Log; disconnect DB; PERFORM Z000-PROGRAM-END. |
16 |
System error — abend | Emergency disconnect; STOP RUN with non-zero RETURN-CODE. |
Note — the pgcobol project uses only codes 0, 1, and 9/99 (EOF). Adopting the full scheme above provides finer-grained diagnostics and removes the ambiguity between error and end-of-data.
7.2 Mandatory Error Paragraph — E000-HANDLE-ERROR[edit]
Every program must implement an E000-HANDLE-ERROR paragraph:
<syntaxhighlight lang="cobol">
E000-HANDLE-ERROR.
MOVE 'E000-HANDLE-ERROR' TO ERR-PARAGRAPH
MOVE ERR-MESSAGE TO UI-SCREEN-MESSAGE
MOVE 'log-message' TO UI-METHODS
CALL 'programui' USING PROGRAM-UI
IF ERR-SEVERITY > 4
PERFORM Z000-PROGRAM-END
END-IF.
</syntaxhighlight>
7.3 Prohibited Patterns[edit]
| Prohibited Pattern | Reason / Compliant Alternative |
|---|---|
STOP RUN without DB disconnect |
Leaves open transactions; always PERFORM Z000-PROGRAM-END first. |
| Ignoring SQLCODE after any EXEC SQL | Silent failures corrupt data; always EVALUATE SQLCODE. |
NEXT SENTENCE (post-COBOL 85) |
Ambiguous scope; use CONTINUE inside a scoped IF / EVALUATE. |
| Empty WHEN OTHER with no comment | Add at minimum: *-- no action required because <reason>
|
GO TO (except structured exit) |
Use PERFORM, EVALUATE, EXIT PARAGRAPH, EXIT PROGRAM. |
ALTER statement |
Prohibited entirely; dynamic paragraph modification is untestable. |
SELECT * in EXEC SQL |
Always name columns; column-order changes silently break host-variable binding. |
7.4 Cleanup Always Attempted[edit]
Termination paragraphs (Z000-series) must always be performed before program exit, regardless of whether prior steps succeeded. This guarantees that database connections and print files are always closed. Guard with conditional logic inside Z000 rather than skipping Z000 itself.