MainframeMaster

COBOL Tutorial

COBOL RAISED - Quick Reference

Progress0 of 0 lessons

Overview

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.

Purpose and Usage

  • Exception detection and checking
  • Conditional error handling
  • Program flow control based on exceptions
  • Error recovery decision making
  • Exception status monitoring

Syntax

The RAISED clause follows specific syntax patterns:

Basic Syntax

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

Exception Checking Patterns

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

Practical Examples

Here are some practical uses of the RAISED clause in COBOL:

Data Processing with Exception Checking

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

File 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
* 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.

Best Practices

  • Check for exceptions immediately after operations that might raise them.
  • Use specific exception names rather than generic checks when possible.
  • Provide appropriate error handling for each type of exception.
  • Consider the order of exception checking for proper error handling.
  • Use RAISED in combination with other error handling mechanisms.
  • Document the exceptions that each operation might raise.

Common Pitfalls

  • Not checking for exceptions after operations that might raise them.
  • Using generic exception checking instead of specific exception names.
  • Not providing appropriate error handling for checked exceptions.
  • Checking for exceptions in the wrong order.
  • Assuming that no exception means success without proper validation.

Test Your Knowledge

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

  • To raise exceptions
  • To check if an exception has been raised
  • To terminate the program
  • To display error messages

2. When is the RAISED clause typically used?

  • Before executing statements
  • After executing statements to check for exceptions
  • During compilation
  • Only in debugging

3. What does RAISED return?

  • A numeric value
  • A boolean condition (true/false)
  • An error message
  • A program status

4. How do you use RAISED in conditional statements?

  • IF RAISED exception-name
  • IF exception-name RAISED
  • CHECK RAISED exception-name
  • TEST RAISED exception-name

5. What is the relationship between RAISE and RAISED?

  • They are the same
  • RAISE creates exceptions, RAISED checks for them
  • RAISED is obsolete
  • They cannot be used together

Frequently Asked Questions