MainframeMaster

COBOL Tutorial

COBOL NOT ON EXCEPTION - Quick Reference

Progress0 of 0 lessons

Overview

The NOT ON EXCEPTION clause in COBOL handles the successful completion of operations. It executes when an operation completes successfully, allowing the program to continue with normal processing flow.

Purpose and Usage

  • Success handling for operations
  • Normal flow processing after successful operations
  • Complementary to ON EXCEPTION for complete error handling
  • Clear separation of success and error logic

Syntax

The NOT ON EXCEPTION clause follows specific syntax patterns:

Basic Syntax

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

NOT ON EXCEPTION handles successful operations.

File Operation Example

cobol
1
2
3
4
5
6
7
8
9
10
* File operation with success 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 PERFORM continue-processing END-EXCEPTION.

Method Invocation Example

cobol
1
2
3
4
5
6
7
8
9
10
* Method invocation with success 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 process-results PERFORM continue-processing END-EXCEPTION.

Practical Examples

Here are some practical uses of NOT ON EXCEPTION in COBOL:

File Processing with Success 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
* Reading file with success 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 ADD 1 TO records-processed DISPLAY "Record processed successfully" END-EXCEPTION. * Writing file with success handling WRITE output-record ON EXCEPTION DISPLAY "Write error: " file-status PERFORM write-error-handling NOT ON EXCEPTION ADD 1 TO records-written DISPLAY "Record written successfully" END-EXCEPTION.

Comprehensive file I/O with success handling.

Object Method Success 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
* Database connection with success 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 PERFORM continue-processing END-EXCEPTION. * Data validation with success 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 PERFORM continue-processing END-EXCEPTION.

Object method success handling for robust operations.

Best Practices

  • Use NOT ON EXCEPTION to handle successful operations explicitly.
  • Provide clear success messages for debugging and monitoring.
  • Perform necessary cleanup or continuation logic in success blocks.
  • Update counters or status indicators for successful operations.
  • Continue normal program flow after successful operations.
  • Test both success and error paths thoroughly.

Common Pitfalls

  • Forgetting to handle successful operations explicitly.
  • Not updating status indicators or counters for successful operations.
  • Missing continuation logic after successful operations.
  • Not providing appropriate success messages for monitoring.
  • Assuming operations will always succeed without proper handling.

Test Your Knowledge

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

  • To handle errors
  • To handle successful operations
  • To terminate the program
  • To skip statements

2. When does NOT ON EXCEPTION execute?

  • When an error occurs
  • When an operation completes successfully
  • Always
  • Never

3. 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

4. Is NOT ON EXCEPTION required when using ON EXCEPTION?

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

5. What should you typically do in NOT ON EXCEPTION blocks?

  • Handle errors
  • Process successful results and continue normal flow
  • Terminate the program
  • Skip to the next statement

Frequently Asked Questions