Exercises — Chapter 10: Defensive Programming

Exercise 10.1: FILE STATUS Checking (Beginner)

Write a program that opens three sequential files (input, output, error log) with full FILE STATUS checking after each OPEN. Use 88-level condition names for common status codes. If any OPEN fails: - Display the file name and status code - Close any files that were successfully opened - Set RETURN-CODE to 16 - STOP RUN

Deliverables: FILE-CHK.cbl

Exercise 10.2: Arithmetic Error Handling (Beginner)

Write a program that reads a file of transaction amounts and accumulates them into a running total. Use ON SIZE ERROR on every ADD operation. When a size error occurs: - Display the transaction that caused the overflow - Log the error - Skip the transaction and continue processing

Use a PIC 9(7)V99 total field deliberately too small, and test with amounts that will cause overflow.

Deliverables: ARITH-CHK.cbl, test data file

Exercise 10.3: Input Validation Framework (Intermediate)

Create a program that reads employee records and validates every field: - Employee ID: must be numeric, 6 digits - Name: must not be blank - Department: must be one of 'ENG', 'MKT', 'FIN', 'HR', 'OPS' - Salary: must be numeric, between 25000 and 500000 - Hire date: must be a valid date (check month, day, and leap year) - Status: must be 'A' (active), 'T' (terminated), or 'L' (leave)

Collect all validation errors for each record (do not stop at the first error). Write valid records to an output file and invalid records to a reject file with all error reasons.

Deliverables: EMP-VALID.cbl, test data with deliberate errors

Exercise 10.4: Graceful Degradation (Intermediate)

Enhance the program from Exercise 10.3 with: - A maximum total error threshold (stop after 200 total errors) - A maximum consecutive error threshold (stop after 15 consecutive errors) - Warning messages when thresholds are at 80% and 90% - Different return codes: 0 (no errors), 4 (some rejects but within threshold), 8 (threshold reached)

Deliverables: EMP-DEGRADE.cbl

Exercise 10.5: Structured Error Logging (Intermediate)

Design and implement a structured error log that writes pipe-delimited records containing: - Timestamp (YYYY-MM-DD HH:MM:SS) - Severity (INFO, WARN, ERROR, FATAL) - Program name - Paragraph name - File status code (if applicable) - Record key (if applicable) - Error message

Write a logging paragraph and use it throughout a file processing program. Include at least 10 different error scenarios that produce log entries.

Deliverables: LOG-DEMO.cbl

Exercise 10.6: DECLARATIVES (Intermediate)

Write a program that uses DECLARATIVES with USE AFTER ERROR PROCEDURE for two files: an input file and an output file. The DECLARATIVE sections should: - Log the file name, operation, and status code - Increment an error counter - Display a diagnostic message

Then write the same program without DECLARATIVES, using only inline FILE STATUS checking. Compare the two approaches in terms of readability and maintainability.

Deliverables: DECL-DEMO.cbl, NODECL-DEMO.cbl

Exercise 10.7: Return Code Propagation (Advanced)

Write a main program that CALLs three subprograms in sequence: 1. VALIDATE-INPUT — validates a file and returns a status code via LINKAGE SECTION 2. PROCESS-DATA — processes validated data and returns a status code 3. GENERATE-REPORT — produces a report and returns a status code

Each subprogram should be able to return: 'OK', 'WN' (warning), 'ER' (error), or 'FT' (fatal). The main program should: - Stop on fatal - Continue on warning or error - Set the final RETURN-CODE based on the worst status received

Deliverables: MAIN-PGM.cbl, VALIDATE.cbl, PROCESS.cbl, REPORT.cbl

Exercise 10.8: Comprehensive Defensive Program (Capstone)

Build a complete transaction processing program that demonstrates every defensive technique from this chapter: - FILE STATUS on all files (minimum 4 files) - ON SIZE ERROR on arithmetic operations - INVALID KEY on indexed file access - Input validation (numeric, range, date, cross-field) - Multi-error collection per record - Error threshold with consecutive error detection - Structured error logging - ABEND handler with file cleanup - Meaningful return codes - Summary statistics at termination

Use the GlobalBank transaction processing scenario as your context. The program should read a sequential transaction file, validate each transaction, look up the account in an indexed file, apply the transaction if valid, and produce a detailed error log and summary report.

Deliverables: TXN-DEF.cbl, all required copybooks, test data files

Reflection Questions

  1. What is the cost-benefit trade-off of checking FILE STATUS after CLOSE operations? Under what circumstances could a failed CLOSE cause data loss?

  2. James Okafor says "the consecutive error threshold is more important than the total error threshold." Do you agree? Why or why not?

  3. Compare COBOL's explicit error handling (FILE STATUS, ON SIZE ERROR) with exception handling in Java or Python (try/catch). What are the advantages and disadvantages of each approach?

  4. How would you test the error handling in a production program? What types of errors are easy to simulate, and which are difficult?