The RAISED clause in COBOL is used to check whether an exception has been raised during program execution. It returns a boolean condition that can be used in conditional statements to determine if a specific exception occurred, enabling conditional error handling and program flow control.
The RAISED clause follows specific syntax patterns:
1234567891011121314151617181920* Basic RAISED clause usage IF exception-name RAISED PERFORM exception-handling END-IF. * RAISED in conditional statements IF validation-error RAISED DISPLAY "Validation error occurred" PERFORM validation-error-handling END-IF. * Multiple exception checking IF file-error RAISED OR data-error RAISED PERFORM general-error-handling END-IF. * RAISED with NOT IF NOT business-rule-error RAISED PERFORM normal-processing END-IF.
RAISED is used in conditional statements to check for exceptions.
1234567891011121314151617181920212223242526272829* Checking for specific exceptions IF invalid-input RAISED DISPLAY "Invalid input detected" PERFORM input-error-handling END-IF. * Checking multiple exceptions IF file-not-found RAISED OR access-denied RAISED DISPLAY "File access problem" PERFORM file-error-handling END-IF. * Checking for no exceptions IF NOT any-exception RAISED DISPLAY "Operation completed successfully" PERFORM success-processing END-IF. * Complex exception checking EVALUATE TRUE WHEN validation-error RAISED PERFORM validation-handling WHEN system-error RAISED PERFORM system-error-handling WHEN business-error RAISED PERFORM business-error-handling WHEN OTHER PERFORM general-error-handling END-EVALUATE.
Here are some practical uses of the RAISED clause in COBOL:
1234567891011121314151617181920212223242526272829303132333435* Data processing with RAISED checking PROCEDURE DIVISION. PROCESS-DATA-RECORDS. * Process each record 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 * Check for processing exceptions IF data-validation-error RAISED DISPLAY "Data validation failed for record" PERFORM validation-error-handling END-IF IF business-rule-violation RAISED DISPLAY "Business rule violated for record" PERFORM business-error-handling END-IF IF NOT any-error RAISED ADD 1 TO records-processed DISPLAY "Record processed successfully" END-IF END-PERFORM. * Final status check IF processing-error RAISED DISPLAY "Processing completed with errors" ELSE DISPLAY "All records processed successfully" END-IF.
Using RAISED to check for exceptions during data processing.
1234567891011121314151617181920212223242526272829303132333435363738394041424344* File operations with RAISED checking PROCEDURE DIVISION. FILE-OPERATIONS. * Open file with exception checking OPEN INPUT data-file. IF file-not-found RAISED DISPLAY "Data file not found" PERFORM file-not-found-handling STOP RUN END-IF. IF access-denied RAISED DISPLAY "Access denied to data file" PERFORM access-denied-handling STOP RUN END-IF. * Read file records 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 * Check for read exceptions IF read-error RAISED DISPLAY "Error reading record" PERFORM read-error-handling END-IF IF data-corruption RAISED DISPLAY "Data corruption detected" PERFORM data-corruption-handling END-IF END-PERFORM. * Close file CLOSE data-file. IF close-error RAISED DISPLAY "Error closing file" PERFORM close-error-handling END-IF.
Using RAISED to check for file operation exceptions.
1. What is the primary purpose of the RAISED clause in COBOL?
2. When is the RAISED clause typically used?
3. What does RAISED return?
4. How do you use RAISED in conditional statements?
5. What is the relationship between RAISE and RAISED?