USE AFTER STANDARD EXCEPTION PROCEDURE is a COBOL feature that allows you to define a centralized exception handling procedure for standard system exceptions. It provides a structured way to handle exceptions that occur during program execution, enabling consistent error recovery and improved program reliability.
USE AFTER STANDARD EXCEPTION PROCEDURE is declared in the ENVIRONMENT DIVISION:
12345678910111213141516171819202122* Basic USE AFTER STANDARD EXCEPTION PROCEDURE ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. USE AFTER STANDARD EXCEPTION PROCEDURE exception-handler. DATA DIVISION. WORKING-STORAGE SECTION. 01 error-message PIC X(100). PROCEDURE DIVISION. MAIN-PROCESS. * Program logic here PERFORM normal-processing. STOP RUN. * Exception handling procedure EXCEPTION-HANDLER. * Handle standard exceptions DISPLAY "Standard exception occurred". PERFORM error-recovery. EXIT PROGRAM.
The exception procedure is automatically called when standard exceptions occur.
12345678910111213141516171819202122232425262728293031323334353637* Comprehensive exception handling EXCEPTION-HANDLER. * Log the exception DISPLAY "Exception occurred at: " FUNCTION CURRENT-DATE. * Determine exception type and handle accordingly IF arithmetic-exception DISPLAY "Arithmetic exception detected" PERFORM arithmetic-error-recovery ELSE IF file-exception DISPLAY "File exception detected" PERFORM file-error-recovery ELSE IF system-exception DISPLAY "System exception detected" PERFORM system-error-recovery ELSE DISPLAY "Unknown exception detected" PERFORM general-error-recovery END-IF. * Perform cleanup PERFORM cleanup-procedures. * Exit program or continue as appropriate EXIT PROGRAM. ARITHMETIC-ERROR-RECOVERY. DISPLAY "Handling arithmetic error". * Specific arithmetic error handling logic. FILE-ERROR-RECOVERY. DISPLAY "Handling file error". * Specific file error handling logic. SYSTEM-ERROR-RECOVERY. DISPLAY "Handling system error". * Specific system error handling logic.
Here are some practical uses of USE AFTER STANDARD EXCEPTION PROCEDURE in COBOL:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364* File processing with standard exception handling ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. USE AFTER STANDARD EXCEPTION PROCEDURE file-exception-handler. DATA DIVISION. WORKING-STORAGE SECTION. 01 file-status PIC XX. 01 error-log PIC X(200). PROCEDURE DIVISION. PROCESS-FILES. * Open and process files OPEN INPUT data-file. PERFORM UNTIL end-of-file READ data-file AT END MOVE "Y" TO end-of-file-flag NOT AT END PERFORM process-record END-READ END-PERFORM. CLOSE data-file. STOP RUN. * Standard exception handler for file operations FILE-EXCEPTION-HANDLER. * Log the exception details MOVE FUNCTION CURRENT-DATE TO error-log. DISPLAY "File exception: " error-log. * Handle specific file exceptions IF file-not-found DISPLAY "File not found - using default file" PERFORM use-default-file ELSE IF access-denied DISPLAY "Access denied - checking permissions" PERFORM check-file-permissions ELSE IF file-corrupted DISPLAY "File corrupted - attempting recovery" PERFORM file-recovery ELSE DISPLAY "Unknown file error - terminating" PERFORM graceful-shutdown END-IF. EXIT PROGRAM. USE-DEFAULT-FILE. * Logic to use default file DISPLAY "Switching to default file". CHECK-FILE-PERMISSIONS. * Logic to check and fix permissions DISPLAY "Checking file permissions". FILE-RECOVERY. * Logic to recover corrupted file DISPLAY "Attempting file recovery". GRACEFUL-SHUTDOWN. * Logic for graceful program termination DISPLAY "Performing graceful shutdown".
Comprehensive file processing with standard exception handling.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364* Arithmetic operations with exception handling ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. USE AFTER STANDARD EXCEPTION PROCEDURE arithmetic-exception-handler. DATA DIVISION. WORKING-STORAGE SECTION. 01 result PIC 9(10)V99. 01 divisor PIC 9(5). 01 dividend PIC 9(5). PROCEDURE DIVISION. CALCULATE-RESULTS. * Perform arithmetic operations MOVE 100 TO dividend. MOVE 5 TO divisor. DIVIDE dividend BY divisor GIVING result. DISPLAY "Result: " result. * Try division by zero (will trigger exception) MOVE 0 TO divisor. DIVIDE dividend BY divisor GIVING result. STOP RUN. * Standard exception handler for arithmetic operations ARITHMETIC-EXCEPTION-HANDLER. * Log arithmetic exception DISPLAY "Arithmetic exception detected". * Handle specific arithmetic exceptions IF division-by-zero DISPLAY "Division by zero detected" PERFORM handle-division-by-zero ELSE IF overflow DISPLAY "Arithmetic overflow detected" PERFORM handle-overflow ELSE IF underflow DISPLAY "Arithmetic underflow detected" PERFORM handle-underflow ELSE DISPLAY "Unknown arithmetic error" PERFORM handle-unknown-arithmetic-error END-IF. EXIT PROGRAM. HANDLE-DIVISION-BY-ZERO. DISPLAY "Setting result to zero for division by zero". MOVE ZERO TO result. HANDLE-OVERFLOW. DISPLAY "Handling arithmetic overflow". MOVE 9999999999 TO result. HANDLE-UNDERFLOW. DISPLAY "Handling arithmetic underflow". MOVE ZERO TO result. HANDLE-UNKNOWN-ARITHMETIC-ERROR. DISPLAY "Handling unknown arithmetic error". MOVE ZERO TO result.
Arithmetic operations with comprehensive exception handling.
1. What is USE AFTER STANDARD EXCEPTION PROCEDURE in COBOL?
2. Where is USE AFTER STANDARD EXCEPTION PROCEDURE declared?
3. What types of exceptions does it handle?
4. When is the exception procedure executed?
5. What is the primary purpose of this feature?