The AT clause represents one of COBOL's most fundamental and versatile conditional processing mechanisms, serving as the cornerstone for exception handling, file processing control, and sophisticated program flow management. Far more than a simple conditional statement, the AT clause embodies COBOL's comprehensive approach to error handling and boundary condition management by providing precise control over program behavior when specific conditions are encountered, including end-of-file situations, invalid key conditions, overflow scenarios, and other exceptional circumstances that require specialized processing logic.
In enterprise computing environments, the AT clause serves as a critical component for robust application design, enabling developers to create resilient systems that handle unexpected conditions gracefully while maintaining data integrity and business continuity. Its capabilities extend far beyond simple error detection to encompass sophisticated exception handling strategies, recovery mechanisms, and conditional processing patterns that are essential for mission-critical applications operating in complex business environments where system reliability and data accuracy are paramount concerns.
The AT clause implements a sophisticated conditional processing and exception handling architecture that enables programs to respond intelligently to various boundary conditions and exceptional circumstances. This architecture encompasses multiple condition detection mechanisms, structured exception handling patterns, and recovery strategies that work together to provide robust, reliable program execution suitable for production business applications that must handle diverse operational scenarios and maintain consistent behavior under varying conditions.
At its core, the AT clause manages the complex interaction between normal program flow and exceptional conditions by providing structured mechanisms for detecting, handling, and recovering from various types of boundary situations. This includes sophisticated condition monitoring that automatically detects end-of-file conditions, invalid key situations, overflow scenarios, and other exceptional circumstances, as well as structured response mechanisms that enable programs to take appropriate action while maintaining data integrity and system stability.
The architectural design of the AT clause reflects COBOL's emphasis on reliability and maintainability. Unlike simple error checking mechanisms in other languages, the AT clause provides comprehensive exception handling capabilities that integrate seamlessly with COBOL's structured programming model, enabling developers to create robust applications that handle exceptional conditions gracefully while maintaining clear, readable code that is easy to understand and maintain over time.
In enterprise environments, the AT clause enables sophisticated exception handling patterns that handle the complex requirements of business applications including transaction rollback scenarios, data validation failures, system resource constraints, and integration with enterprise monitoring and alerting systems. These patterns must balance system resilience with performance requirements while supporting the reliability and maintainability requirements of modern business systems.
Modern enterprise applications implement layered exception handling architectures where AT clauses work in conjunction with logging frameworks, monitoring systems, and business process management platforms. This layered approach enables applications to handle exceptional conditions at multiple levels while maintaining consistent error handling policies and providing comprehensive visibility into system behavior and operational characteristics.
The integration of AT clauses with contemporary application architectures enables sophisticated resilience patterns including circuit breaker implementations, retry mechanisms with exponential backoff, and graceful degradation strategies. These patterns support the availability and reliability requirements of modern business systems while leveraging COBOL's proven exception handling capabilities and structured programming features.
The AT clause's performance characteristics are crucial for applications that must handle high transaction volumes, maintain responsive user interfaces, and provide consistent service levels even when encountering exceptional conditions. Performance optimization involves careful design of exception handling logic, efficient condition detection mechanisms, and strategic use of recovery procedures that minimize overhead while maximizing system reliability and user experience.
Advanced performance management includes predictive exception handling where potential problems are detected and addressed before they impact system operation, intelligent retry mechanisms that adapt to system conditions and failure patterns, and dynamic resource allocation that ensures adequate system resources are available for exception handling and recovery operations. These capabilities enable COBOL applications to maintain high performance even when dealing with complex exception scenarios.
Reliability planning for AT clause usage requires understanding both normal and exceptional operational patterns, failure mode analysis, and recovery time objectives. Modern implementations support comprehensive monitoring and alerting that provides real-time visibility into exception handling performance and enables proactive system management that prevents minor issues from becoming major operational problems.
The AT clause provides multiple syntax forms designed to handle different conditional processing scenarios and exception handling requirements. Understanding these variations and their appropriate applications is crucial for creating robust, maintainable programs that can handle the diverse operational conditions encountered in enterprise environments.
12345678910111213READ file-name AT END PERFORM END-OF-FILE-PROCESSING NOT AT END PERFORM PROCESS-RECORD END-READ. *> Alternative syntax READ file-name AT END MOVE 'Y' TO EOF-FLAG DISPLAY 'End of file reached' END-READ.
End-of-file processing provides structured handling of file boundary conditions with automatic detection and appropriate response mechanisms.
123456789101112READ indexed-file KEY IS record-key INVALID KEY PERFORM HANDLE-INVALID-KEY NOT INVALID KEY PERFORM PROCESS-VALID-RECORD END-READ. WRITE record-name INVALID KEY DISPLAY 'Duplicate key error: ' record-key PERFORM LOG-ERROR END-WRITE.
Invalid key handling manages key-related exceptions in indexed file operations with comprehensive error detection and recovery capabilities.
123456789101112ADD amount TO total-amount ON SIZE ERROR PERFORM HANDLE-OVERFLOW NOT ON SIZE ERROR PERFORM CONTINUE-PROCESSING END-ADD. COMPUTE result = value1 * value2 ON SIZE ERROR DISPLAY 'Arithmetic overflow detected' MOVE HIGH-VALUES TO result END-COMPUTE.
Overflow condition handling manages arithmetic and data size exceptions with automatic detection and appropriate response strategies.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632IDENTIFICATION DIVISION. PROGRAM-ID. COMPREHENSIVE-AT-DEMO. *> Comprehensive demonstration of AT clause applications *> Covering exception handling, file processing, and control flow ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "INPUT.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS INPUT-FILE-STATUS. SELECT OUTPUT-FILE ASSIGN TO "OUTPUT.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS OUTPUT-FILE-STATUS. SELECT MASTER-FILE ASSIGN TO "MASTER.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS MASTER-KEY FILE STATUS IS MASTER-FILE-STATUS. SELECT ERROR-LOG ASSIGN TO "ERROR.LOG" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS ERROR-FILE-STATUS. DATA DIVISION. FILE SECTION. FD INPUT-FILE RECORD CONTAINS 100 CHARACTERS. 01 INPUT-RECORD. 05 INPUT-ID PIC X(10). 05 INPUT-NAME PIC X(30). 05 INPUT-AMOUNT PIC 9(8)V99. 05 INPUT-TYPE PIC X(5). 05 FILLER PIC X(45). FD OUTPUT-FILE RECORD CONTAINS 120 CHARACTERS. 01 OUTPUT-RECORD. 05 OUTPUT-ID PIC X(10). 05 OUTPUT-NAME PIC X(30). 05 OUTPUT-PROCESSED-AMOUNT PIC 9(10)V99. 05 OUTPUT-STATUS PIC X(10). 05 OUTPUT-TIMESTAMP PIC X(14). 05 FILLER PIC X(44). FD MASTER-FILE RECORD CONTAINS 200 CHARACTERS. 01 MASTER-RECORD. 05 MASTER-KEY PIC X(10). 05 MASTER-NAME PIC X(50). 05 MASTER-BALANCE PIC S9(10)V99. 05 MASTER-STATUS PIC X(5). 05 MASTER-LAST-UPDATE PIC X(8). 05 FILLER PIC X(125). FD ERROR-LOG RECORD CONTAINS 150 CHARACTERS. 01 ERROR-RECORD. 05 ERROR-TIMESTAMP PIC X(14). 05 ERROR-TYPE PIC X(20). 05 ERROR-DESCRIPTION PIC X(100). 05 ERROR-RECORD-ID PIC X(10). 05 FILLER PIC X(6). WORKING-STORAGE SECTION. *> File status variables 01 FILE-STATUS-CODES. 05 INPUT-FILE-STATUS PIC X(2). 88 INPUT-FILE-OK VALUE "00". 88 INPUT-EOF VALUE "10". 88 INPUT-FILE-ERROR VALUE "30" THRU "99". 05 OUTPUT-FILE-STATUS PIC X(2). 88 OUTPUT-FILE-OK VALUE "00". 88 OUTPUT-FILE-ERROR VALUE "30" THRU "99". 05 MASTER-FILE-STATUS PIC X(2). 88 MASTER-FILE-OK VALUE "00". 88 MASTER-NOT-FOUND VALUE "23". 88 MASTER-DUPLICATE VALUE "22". 88 MASTER-FILE-ERROR VALUE "30" THRU "99". 05 ERROR-FILE-STATUS PIC X(2). 88 ERROR-FILE-OK VALUE "00". 88 ERROR-FILE-ERROR VALUE "30" THRU "99". *> Control flags and counters 01 PROCESSING-CONTROLS. 05 EOF-FLAG PIC X VALUE 'N'. 88 END-OF-FILE VALUE 'Y'. 88 NOT-END-OF-FILE VALUE 'N'. 05 ERROR-FLAG PIC X VALUE 'N'. 88 ERRORS-OCCURRED VALUE 'Y'. 88 NO-ERRORS VALUE 'N'. 05 RECORDS-READ PIC 9(8) VALUE ZERO. 05 RECORDS-PROCESSED PIC 9(8) VALUE ZERO. 05 RECORDS-WRITTEN PIC 9(8) VALUE ZERO. 05 ERRORS-ENCOUNTERED PIC 9(5) VALUE ZERO. 05 INVALID-KEYS PIC 9(5) VALUE ZERO. 05 OVERFLOW-CONDITIONS PIC 9(5) VALUE ZERO. *> Working variables for calculations 01 CALCULATION-VARIABLES. 05 WS-TOTAL-AMOUNT PIC S9(12)V99 VALUE ZERO. 05 WS-PROCESSED-AMOUNT PIC S9(12)V99. 05 WS-MULTIPLIER PIC 9(3)V99 VALUE 1.05. 05 WS-MAXIMUM-AMOUNT PIC 9(10)V99 VALUE 999999999.99. 05 WS-MINIMUM-AMOUNT PIC 9(8)V99 VALUE 0.01. *> Date and time variables 01 WS-CURRENT-DATETIME. 05 WS-CURRENT-DATE PIC X(8). 05 WS-CURRENT-TIME PIC X(6). 05 WS-TIMESTAMP PIC X(14). *> Error handling variables 01 ERROR-HANDLING-VARIABLES. 05 WS-ERROR-COUNT PIC 9(5) VALUE ZERO. 05 WS-ERROR-TYPE PIC X(20). 05 WS-ERROR-DESCRIPTION PIC X(100). 05 WS-RETRY-COUNT PIC 9(2) VALUE ZERO. 05 WS-MAX-RETRIES PIC 9(2) VALUE 3. PROCEDURE DIVISION. MAIN-PROCESSING. DISPLAY "=== Comprehensive AT Clause Demonstration ===". DISPLAY " ". PERFORM INITIALIZE-PROCESSING PERFORM OPEN-FILES PERFORM PROCESS-INPUT-FILE PERFORM DEMONSTRATE-INDEXED-FILE-OPERATIONS PERFORM DEMONSTRATE-ARITHMETIC-EXCEPTIONS PERFORM DEMONSTRATE-ADVANCED-EXCEPTION-HANDLING PERFORM CLOSE-FILES PERFORM DISPLAY-FINAL-STATISTICS DISPLAY " ". DISPLAY "AT clause demonstration completed successfully". STOP RUN. INITIALIZE-PROCESSING. DISPLAY "1. Initializing Exception Handling Processing:". DISPLAY " ==========================================". *> Get current date and time ACCEPT WS-CURRENT-DATE FROM DATE YYYYMMDD. ACCEPT WS-CURRENT-TIME FROM TIME. STRING WS-CURRENT-DATE WS-CURRENT-TIME DELIMITED BY SIZE INTO WS-TIMESTAMP. *> Initialize control variables SET NOT-END-OF-FILE TO TRUE. SET NO-ERRORS TO TRUE. MOVE ZERO TO RECORDS-READ RECORDS-PROCESSED RECORDS-WRITTEN ERRORS-ENCOUNTERED INVALID-KEYS OVERFLOW-CONDITIONS. DISPLAY " Processing initialized at: " WS-TIMESTAMP. DISPLAY " Error handling mechanisms activated". DISPLAY " ". OPEN-FILES. DISPLAY "2. Opening Files with Exception Handling:". DISPLAY " =======================================". *> Open input file with error handling OPEN INPUT INPUT-FILE. EVALUATE INPUT-FILE-STATUS WHEN "00" DISPLAY " Input file opened successfully" WHEN "35" DISPLAY " WARNING: Input file not found" PERFORM LOG-ERROR-CONDITION WHEN OTHER DISPLAY " ERROR opening input file: " INPUT-FILE-STATUS PERFORM LOG-ERROR-CONDITION PERFORM TERMINATE-PROCESSING END-EVALUATE. *> Open output file with error handling OPEN OUTPUT OUTPUT-FILE. IF NOT OUTPUT-FILE-OK DISPLAY " ERROR opening output file: " OUTPUT-FILE-STATUS PERFORM LOG-ERROR-CONDITION PERFORM TERMINATE-PROCESSING ELSE DISPLAY " Output file opened successfully" END-IF. *> Open master file with error handling OPEN I-O MASTER-FILE. EVALUATE MASTER-FILE-STATUS WHEN "00" DISPLAY " Master file opened successfully" WHEN "35" DISPLAY " Master file not found - creating new" OPEN OUTPUT MASTER-FILE CLOSE MASTER-FILE OPEN I-O MASTER-FILE WHEN OTHER DISPLAY " ERROR opening master file: " MASTER-FILE-STATUS PERFORM LOG-ERROR-CONDITION END-EVALUATE. *> Open error log file OPEN OUTPUT ERROR-LOG. IF ERROR-FILE-OK DISPLAY " Error log file opened successfully" ELSE DISPLAY " WARNING: Cannot open error log: " ERROR-FILE-STATUS END-IF. DISPLAY " ". PROCESS-INPUT-FILE. DISPLAY "3. Processing Input File with AT END Handling:". DISPLAY " ============================================". PERFORM READ-NEXT-RECORD. PERFORM UNTIL END-OF-FILE OR ERRORS-OCCURRED PERFORM PROCESS-SINGLE-RECORD PERFORM READ-NEXT-RECORD END-PERFORM. DISPLAY " Input file processing completed". DISPLAY " Records read: " RECORDS-READ. DISPLAY " Records processed: " RECORDS-PROCESSED. DISPLAY " ". READ-NEXT-RECORD. READ INPUT-FILE AT END SET END-OF-FILE TO TRUE DISPLAY " End of input file reached" NOT AT END ADD 1 TO RECORDS-READ IF RECORDS-READ = 1 DISPLAY " First record read successfully" END-IF END-READ. *> Check for read errors IF INPUT-FILE-ERROR DISPLAY " ERROR reading input file: " INPUT-FILE-STATUS PERFORM LOG-ERROR-CONDITION SET ERRORS-OCCURRED TO TRUE END-IF. PROCESS-SINGLE-RECORD. *> Validate input record IF INPUT-ID = SPACES OR INPUT-AMOUNT = ZERO DISPLAY " WARNING: Invalid record data for ID: " INPUT-ID PERFORM LOG-INVALID-RECORD EXIT PARAGRAPH END-IF. *> Process the record with exception handling PERFORM CALCULATE-PROCESSED-AMOUNT PERFORM UPDATE-MASTER-RECORD PERFORM WRITE-OUTPUT-RECORD ADD 1 TO RECORDS-PROCESSED. CALCULATE-PROCESSED-AMOUNT. *> Calculate processed amount with overflow handling COMPUTE WS-PROCESSED-AMOUNT = INPUT-AMOUNT * WS-MULTIPLIER ON SIZE ERROR DISPLAY " OVERFLOW: Amount too large for ID: " INPUT-ID MOVE WS-MAXIMUM-AMOUNT TO WS-PROCESSED-AMOUNT ADD 1 TO OVERFLOW-CONDITIONS PERFORM LOG-OVERFLOW-CONDITION NOT ON SIZE ERROR *> Check if result is within business limits IF WS-PROCESSED-AMOUNT > WS-MAXIMUM-AMOUNT DISPLAY " WARNING: Processed amount exceeds limit for ID: " INPUT-ID MOVE WS-MAXIMUM-AMOUNT TO WS-PROCESSED-AMOUNT END-IF END-COMPUTE. *> Add to total with overflow protection ADD WS-PROCESSED-AMOUNT TO WS-TOTAL-AMOUNT ON SIZE ERROR DISPLAY " OVERFLOW: Total amount overflow detected" SUBTRACT WS-PROCESSED-AMOUNT FROM WS-TOTAL-AMOUNT ADD 1 TO OVERFLOW-CONDITIONS PERFORM LOG-OVERFLOW-CONDITION END-ADD. UPDATE-MASTER-RECORD. *> Read master record with invalid key handling MOVE INPUT-ID TO MASTER-KEY. READ MASTER-FILE KEY IS MASTER-KEY INVALID KEY PERFORM CREATE-NEW-MASTER-RECORD NOT INVALID KEY PERFORM UPDATE-EXISTING-MASTER-RECORD END-READ. CREATE-NEW-MASTER-RECORD. DISPLAY " Creating new master record for ID: " INPUT-ID. MOVE INPUT-ID TO MASTER-KEY. MOVE INPUT-NAME TO MASTER-NAME. MOVE WS-PROCESSED-AMOUNT TO MASTER-BALANCE. MOVE "ACTIV" TO MASTER-STATUS. MOVE WS-CURRENT-DATE TO MASTER-LAST-UPDATE. WRITE MASTER-RECORD INVALID KEY DISPLAY " ERROR: Cannot create master record: " MASTER-KEY ADD 1 TO INVALID-KEYS PERFORM LOG-INVALID-KEY-CONDITION END-WRITE. UPDATE-EXISTING-MASTER-RECORD. DISPLAY " Updating existing master record for ID: " INPUT-ID. ADD WS-PROCESSED-AMOUNT TO MASTER-BALANCE ON SIZE ERROR DISPLAY " OVERFLOW: Master balance overflow for ID: " INPUT-ID SUBTRACT WS-PROCESSED-AMOUNT FROM MASTER-BALANCE ADD 1 TO OVERFLOW-CONDITIONS PERFORM LOG-OVERFLOW-CONDITION END-ADD. MOVE WS-CURRENT-DATE TO MASTER-LAST-UPDATE. REWRITE MASTER-RECORD INVALID KEY DISPLAY " ERROR: Cannot update master record: " MASTER-KEY ADD 1 TO INVALID-KEYS PERFORM LOG-INVALID-KEY-CONDITION END-REWRITE. WRITE-OUTPUT-RECORD. MOVE INPUT-ID TO OUTPUT-ID. MOVE INPUT-NAME TO OUTPUT-NAME. MOVE WS-PROCESSED-AMOUNT TO OUTPUT-PROCESSED-AMOUNT. MOVE "PROCESSED" TO OUTPUT-STATUS. MOVE WS-TIMESTAMP TO OUTPUT-TIMESTAMP. WRITE OUTPUT-RECORD. IF OUTPUT-FILE-OK ADD 1 TO RECORDS-WRITTEN ELSE DISPLAY " ERROR writing output record: " OUTPUT-FILE-STATUS PERFORM LOG-ERROR-CONDITION END-IF. DEMONSTRATE-INDEXED-FILE-OPERATIONS. DISPLAY "4. Indexed File Operations with Exception Handling:". DISPLAY " ================================================". PERFORM DEMONSTRATE-RANDOM-READ-OPERATIONS PERFORM DEMONSTRATE-SEQUENTIAL-READ-OPERATIONS PERFORM DEMONSTRATE-DELETE-OPERATIONS. DEMONSTRATE-RANDOM-READ-OPERATIONS. DISPLAY " Random Read Operations:". *> Attempt to read specific records MOVE "TEST000001" TO MASTER-KEY. READ MASTER-FILE KEY IS MASTER-KEY INVALID KEY DISPLAY " Record not found: " MASTER-KEY NOT INVALID KEY DISPLAY " Found record: " MASTER-KEY " Balance: $" MASTER-BALANCE END-READ. MOVE "NONEXIST01" TO MASTER-KEY. READ MASTER-FILE KEY IS MASTER-KEY INVALID KEY DISPLAY " Expected: Record not found: " MASTER-KEY NOT INVALID KEY DISPLAY " Unexpected: Found record: " MASTER-KEY END-READ. DEMONSTRATE-SEQUENTIAL-READ-OPERATIONS. DISPLAY " Sequential Read Operations:". *> Start sequential reading from beginning MOVE LOW-VALUES TO MASTER-KEY. START MASTER-FILE KEY IS GREATER THAN MASTER-KEY INVALID KEY DISPLAY " No records in master file" NOT INVALID KEY PERFORM READ-NEXT-MASTER-RECORD PERFORM 5 TIMES READ MASTER-FILE NEXT RECORD AT END DISPLAY " End of master file reached" EXIT PERFORM NOT AT END PERFORM READ-NEXT-MASTER-RECORD END-READ END-PERFORM END-START. READ-NEXT-MASTER-RECORD. DISPLAY " Master record: " MASTER-KEY " " MASTER-NAME(1:20). DEMONSTRATE-DELETE-OPERATIONS. DISPLAY " Delete Operations:". *> Attempt to delete a record MOVE "DELETE001" TO MASTER-KEY. READ MASTER-FILE KEY IS MASTER-KEY INVALID KEY DISPLAY " Cannot delete - record not found: " MASTER-KEY NOT INVALID KEY DELETE MASTER-FILE RECORD INVALID KEY DISPLAY " ERROR deleting record: " MASTER-KEY NOT INVALID KEY DISPLAY " Record deleted successfully: " MASTER-KEY END-DELETE END-READ. DEMONSTRATE-ARITHMETIC-EXCEPTIONS. DISPLAY "5. Arithmetic Exception Handling:". DISPLAY " ===============================". PERFORM DEMONSTRATE-OVERFLOW-CONDITIONS PERFORM DEMONSTRATE-DIVISION-BY-ZERO PERFORM DEMONSTRATE-SIZE-ERROR-HANDLING. DEMONSTRATE-OVERFLOW-CONDITIONS. DISPLAY " Overflow Condition Demonstrations:". *> Demonstrate addition overflow MOVE 999999999.99 TO WS-PROCESSED-AMOUNT. ADD 1000.00 TO WS-PROCESSED-AMOUNT ON SIZE ERROR DISPLAY " Addition overflow detected and handled" ADD 1 TO OVERFLOW-CONDITIONS NOT ON SIZE ERROR DISPLAY " Addition completed: " WS-PROCESSED-AMOUNT END-ADD. *> Demonstrate multiplication overflow MOVE 999999.99 TO WS-PROCESSED-AMOUNT. MULTIPLY 1000 BY WS-PROCESSED-AMOUNT ON SIZE ERROR DISPLAY " Multiplication overflow detected and handled" ADD 1 TO OVERFLOW-CONDITIONS NOT ON SIZE ERROR DISPLAY " Multiplication completed: " WS-PROCESSED-AMOUNT END-MULTIPLY. DEMONSTRATE-DIVISION-BY-ZERO. DISPLAY " Division by Zero Handling:". MOVE ZERO TO WS-MULTIPLIER. DIVIDE 1000 BY WS-MULTIPLIER GIVING WS-PROCESSED-AMOUNT ON SIZE ERROR DISPLAY " Division by zero detected and handled" MOVE 1.00 TO WS-MULTIPLIER MOVE ZERO TO WS-PROCESSED-AMOUNT NOT ON SIZE ERROR DISPLAY " Division completed: " WS-PROCESSED-AMOUNT END-DIVIDE. DEMONSTRATE-SIZE-ERROR-HANDLING. DISPLAY " Size Error Handling:". *> Demonstrate field size limitations MOVE 12345678901234567890 TO WS-PROCESSED-AMOUNT ON SIZE ERROR DISPLAY " Size error detected - value too large" MOVE 999999999.99 TO WS-PROCESSED-AMOUNT NOT ON SIZE ERROR DISPLAY " Value assigned successfully" END-MOVE. DEMONSTRATE-ADVANCED-EXCEPTION-HANDLING. DISPLAY "6. Advanced Exception Handling Patterns:". DISPLAY " ======================================". PERFORM DEMONSTRATE-RETRY-LOGIC PERFORM DEMONSTRATE-GRACEFUL-DEGRADATION PERFORM DEMONSTRATE-RESOURCE-CLEANUP. DEMONSTRATE-RETRY-LOGIC. DISPLAY " Retry Logic Implementation:". MOVE ZERO TO WS-RETRY-COUNT. PERFORM UNTIL WS-RETRY-COUNT >= WS-MAX-RETRIES ADD 1 TO WS-RETRY-COUNT *> Simulate an operation that might fail IF WS-RETRY-COUNT < 3 DISPLAY " Attempt " WS-RETRY-COUNT " failed - retrying" ELSE DISPLAY " Attempt " WS-RETRY-COUNT " succeeded" EXIT PERFORM END-IF END-PERFORM. IF WS-RETRY-COUNT >= WS-MAX-RETRIES DISPLAY " Maximum retries exceeded - operation failed" END-IF. DEMONSTRATE-GRACEFUL-DEGRADATION. DISPLAY " Graceful Degradation:". *> Simulate a service that might be unavailable DISPLAY " Primary service unavailable - using fallback" DISPLAY " Fallback processing completed successfully" DISPLAY " System continues with reduced functionality". DEMONSTRATE-RESOURCE-CLEANUP. DISPLAY " Resource Cleanup:". DISPLAY " Cleaning up temporary resources" DISPLAY " Releasing file locks" DISPLAY " Freeing memory allocations" DISPLAY " Resource cleanup completed". CLOSE-FILES. DISPLAY "7. Closing Files with Exception Handling:". DISPLAY " =======================================". CLOSE INPUT-FILE. IF NOT INPUT-FILE-OK DISPLAY " WARNING: Error closing input file: " INPUT-FILE-STATUS ELSE DISPLAY " Input file closed successfully" END-IF. CLOSE OUTPUT-FILE. IF NOT OUTPUT-FILE-OK DISPLAY " WARNING: Error closing output file: " OUTPUT-FILE-STATUS ELSE DISPLAY " Output file closed successfully" END-IF. CLOSE MASTER-FILE. IF NOT MASTER-FILE-OK DISPLAY " WARNING: Error closing master file: " MASTER-FILE-STATUS ELSE DISPLAY " Master file closed successfully" END-IF. CLOSE ERROR-LOG. IF NOT ERROR-FILE-OK DISPLAY " WARNING: Error closing error log: " ERROR-FILE-STATUS ELSE DISPLAY " Error log closed successfully" END-IF. DISPLAY " ". DISPLAY-FINAL-STATISTICS. DISPLAY "8. Final Processing Statistics:". DISPLAY " ============================". DISPLAY " Records read: " RECORDS-READ. DISPLAY " Records processed: " RECORDS-PROCESSED. DISPLAY " Records written: " RECORDS-WRITTEN. DISPLAY " Total amount processed: $" WS-TOTAL-AMOUNT. DISPLAY " Errors encountered: " ERRORS-ENCOUNTERED. DISPLAY " Invalid key conditions: " INVALID-KEYS. DISPLAY " Overflow conditions: " OVERFLOW-CONDITIONS. IF ERRORS-ENCOUNTERED > 0 OR INVALID-KEYS > 0 OR OVERFLOW-CONDITIONS > 0 DISPLAY " ** ATTENTION: Exception conditions were encountered **" DISPLAY " Review error log for detailed information" ELSE DISPLAY " All processing completed without exceptions" END-IF. LOG-ERROR-CONDITION. ADD 1 TO ERRORS-ENCOUNTERED. SET ERRORS-OCCURRED TO TRUE. MOVE WS-TIMESTAMP TO ERROR-TIMESTAMP. MOVE "FILE ERROR" TO ERROR-TYPE. MOVE "File operation error encountered" TO ERROR-DESCRIPTION. MOVE SPACES TO ERROR-RECORD-ID. IF ERROR-FILE-OK WRITE ERROR-RECORD END-IF. LOG-INVALID-RECORD. ADD 1 TO ERRORS-ENCOUNTERED. MOVE WS-TIMESTAMP TO ERROR-TIMESTAMP. MOVE "DATA ERROR" TO ERROR-TYPE. MOVE "Invalid record data detected" TO ERROR-DESCRIPTION. MOVE INPUT-ID TO ERROR-RECORD-ID. IF ERROR-FILE-OK WRITE ERROR-RECORD END-IF. LOG-INVALID-KEY-CONDITION. MOVE WS-TIMESTAMP TO ERROR-TIMESTAMP. MOVE "INVALID KEY" TO ERROR-TYPE. MOVE "Invalid key condition encountered" TO ERROR-DESCRIPTION. MOVE MASTER-KEY TO ERROR-RECORD-ID. IF ERROR-FILE-OK WRITE ERROR-RECORD END-IF. LOG-OVERFLOW-CONDITION. MOVE WS-TIMESTAMP TO ERROR-TIMESTAMP. MOVE "OVERFLOW" TO ERROR-TYPE. MOVE "Arithmetic overflow condition detected" TO ERROR-DESCRIPTION. MOVE INPUT-ID TO ERROR-RECORD-ID. IF ERROR-FILE-OK WRITE ERROR-RECORD END-IF. TERMINATE-PROCESSING. DISPLAY " CRITICAL ERROR: Terminating processing". PERFORM CLOSE-FILES. STOP RUN.