MainframeMaster

COBOL Tutorial

COBOL ON EXCEPTION - Quick Reference

Progress0 of 0 lessons

Overview

The ON EXCEPTION clause in COBOL provides error handling and recovery mechanisms. It allows programs to respond to errors and exceptions that occur during execution, enabling graceful error handling instead of program termination.

Purpose and Usage

  • Error handling and recovery
  • Graceful failure instead of program termination
  • Robust programming practices
  • User-friendly error messages

Syntax

The ON EXCEPTION clause follows specific syntax patterns:

Basic Syntax

cobol
1
2
3
4
5
6
7
8
* Basic ON EXCEPTION syntax statement ON EXCEPTION * Exception handling code PERFORM error-handling NOT ON EXCEPTION * Success handling code PERFORM success-processing END-EXCEPTION.

ON EXCEPTION handles errors, NOT ON EXCEPTION handles success.

File Operation Example

cobol
1
2
3
4
5
6
7
8
9
* File operation with exception handling OPEN INPUT data-file ON EXCEPTION DISPLAY "Error opening file: " data-file PERFORM file-error-handling NOT ON EXCEPTION DISPLAY "File opened successfully" PERFORM process-file END-EXCEPTION.

Method Invocation Example

cobol
1
2
3
4
5
6
7
8
9
* Method invocation with exception handling INVOKE object "method-name" USING parameter ON EXCEPTION DISPLAY "Method call failed" PERFORM method-error-handling NOT ON EXCEPTION DISPLAY "Method executed successfully" PERFORM continue-processing END-EXCEPTION.

Practical Examples

Here are some practical uses of ON EXCEPTION in COBOL:

File Processing with Error Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* Reading file with exception handling READ data-file ON EXCEPTION IF file-status = "10" DISPLAY "End of file reached" PERFORM end-of-file-processing ELSE DISPLAY "Read error: " file-status PERFORM read-error-handling END-IF NOT ON EXCEPTION PERFORM process-record END-EXCEPTION. * Writing file with exception handling WRITE output-record ON EXCEPTION DISPLAY "Write error: " file-status PERFORM write-error-handling NOT ON EXCEPTION ADD 1 TO records-written END-EXCEPTION.

Comprehensive file I/O error handling.

Object Method Error Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* Database connection with exception handling INVOKE database "Connect" USING connection-string ON EXCEPTION DISPLAY "Database connection failed" PERFORM database-error-handling MOVE "N" TO connection-success NOT ON EXCEPTION DISPLAY "Database connected successfully" MOVE "Y" TO connection-success PERFORM database-operations END-EXCEPTION. * Data validation with exception handling INVOKE validator "ValidateData" USING input-data ON EXCEPTION DISPLAY "Data validation failed" PERFORM validation-error-handling NOT ON EXCEPTION DISPLAY "Data validation successful" PERFORM process-valid-data END-EXCEPTION.

Object method error handling for robust operations.

Best Practices

  • Always use ON EXCEPTION for critical operations that might fail.
  • Provide meaningful error messages to help with debugging.
  • Implement appropriate error recovery mechanisms.
  • Log errors for troubleshooting and monitoring.
  • Consider the user experience when handling errors.
  • Test exception handling paths thoroughly.

Common Pitfalls

  • Not handling all possible error conditions.
  • Providing generic error messages that don\'t help with debugging.
  • Forgetting to implement proper error recovery.
  • Not testing exception handling code paths.
  • Ignoring error conditions that could affect program stability.

Test Your Knowledge

1. What is the primary purpose of the ON EXCEPTION clause in COBOL?

  • To handle errors and exceptions
  • To terminate the program
  • To skip statements
  • To create loops

2. Where is ON EXCEPTION typically used?

  • In file operations
  • In method calls
  • In both file operations and method calls
  • Only in arithmetic operations

3. What happens when an exception occurs and ON EXCEPTION is specified?

  • The program terminates
  • Control transfers to the exception handling code
  • The statement is retried
  • Nothing happens

4. What is the relationship between ON EXCEPTION and NOT ON EXCEPTION?

  • They are the same
  • ON EXCEPTION handles errors, NOT ON EXCEPTION handles success
  • NOT ON EXCEPTION is obsolete
  • They cannot be used together

5. Is ON EXCEPTION required for all operations?

  • Yes, always
  • No, it is optional but recommended
  • Only for file operations
  • Only for method calls

Frequently Asked Questions