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.
The NOT ON EXCEPTION clause follows specific syntax patterns:
12345678* 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.
12345678910* 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.
12345678910* 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.
Here are some practical uses of NOT ON EXCEPTION in COBOL:
12345678910111213141516171819202122232425* 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.
1234567891011121314151617181920212223* 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.
1. What is the primary purpose of the NOT ON EXCEPTION clause in COBOL?
2. When does NOT ON EXCEPTION execute?
3. What is the relationship between ON EXCEPTION and NOT ON EXCEPTION?
4. Is NOT ON EXCEPTION required when using ON EXCEPTION?
5. What should you typically do in NOT ON EXCEPTION blocks?