MainframeMaster

COBOL Tutorial

COBOL USE AFTER STANDARD EXCEPTION PROCEDURE - Quick Reference

Progress0 of 0 lessons

Overview

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.

Purpose and Usage

  • Centralized exception handling for standard exceptions
  • Consistent error recovery mechanisms
  • Improved program reliability
  • Standardized error responses
  • System-level error management

Syntax

USE AFTER STANDARD EXCEPTION PROCEDURE is declared in the ENVIRONMENT DIVISION:

Basic Declaration

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* 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.

Exception Handling Implementation

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
* 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.

Practical Examples

Here are some practical uses of USE AFTER STANDARD EXCEPTION PROCEDURE in COBOL:

File Processing with Exception Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
* 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.

Arithmetic Operations with Exception Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
* 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.

Best Practices

  • Use this feature for centralized exception handling in production applications.
  • Implement specific handling for different types of exceptions.
  • Provide meaningful error messages and logging.
  • Perform appropriate cleanup in exception handlers.
  • Consider the impact on program flow and termination.
  • Test exception handling thoroughly.

Common Pitfalls

  • Not handling all possible exception types.
  • Providing generic error messages that don\'t help with debugging.
  • Not performing proper cleanup in exception handlers.
  • Infinite loops in exception handling procedures.
  • Not considering the impact on program termination.

Test Your Knowledge

1. What is USE AFTER STANDARD EXCEPTION PROCEDURE in COBOL?

  • A file operation
  • An exception handling mechanism
  • A data declaration
  • A program structure

2. Where is USE AFTER STANDARD EXCEPTION PROCEDURE declared?

  • In the PROCEDURE DIVISION
  • In the DATA DIVISION
  • In the ENVIRONMENT DIVISION
  • In the WORKING-STORAGE SECTION

3. What types of exceptions does it handle?

  • Only file exceptions
  • Only arithmetic exceptions
  • Standard system exceptions
  • Only user-defined exceptions

4. When is the exception procedure executed?

  • Before program execution
  • After a standard exception occurs
  • During compilation
  • At program termination

5. What is the primary purpose of this feature?

  • To improve performance
  • To provide centralized exception handling
  • To reduce memory usage
  • To simplify program logic

Frequently Asked Questions