MainframeMaster

COBOL Tutorial

COBOL EXCEPTION-OBJECT Clause - Quick Reference

Progress0 of 0 lessons

Overview

The EXCEPTION-OBJECT clause is used in object-oriented COBOL to define and work with exception objects that encapsulate error information and provide structured exception handling capabilities.

Purpose and Usage

  • Exception encapsulation - Encapsulate error information in objects
  • Structured error handling - Provide structured exception handling
  • Error context preservation - Preserve error context and details
  • Object-oriented error management - Use object-oriented principles for error handling
  • Exception inheritance - Support exception class inheritance

Exception Object Concept

Exception Object: [Error Information] → [Exception Methods] → [Handling Logic]
Exception Flow: [Exception Occurs] → [Exception Object Created] → [Exception Handled]
Object Features: [Inheritance] [Polymorphism] [Encapsulation] [Methods]
EXCEPTION-OBJECT provides object-oriented exception handling capabilities

EXCEPTION-OBJECT enables structured and object-oriented exception handling in COBOL.

Syntax

The EXCEPTION-OBJECT clause follows specific syntax patterns and is used within exception handling sections and object definitions in object-oriented COBOL.

Basic Syntax

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
* Basic EXCEPTION-OBJECT clause syntax EXCEPTION-OBJECT exception-name. * Exception object definition END EXCEPTION-OBJECT. * Examples EXCEPTION-OBJECT FileException. * File-related exception handling END EXCEPTION-OBJECT. EXCEPTION-OBJECT DataException. * Data-related exception handling END EXCEPTION-OBJECT. EXCEPTION-OBJECT SystemException. * System-related exception handling END EXCEPTION-OBJECT. * Complete example with methods EXCEPTION-OBJECT CustomException. DATA DIVISION. WORKING-STORAGE SECTION. 01 ERROR-CODE PIC 9(4). 01 ERROR-MESSAGE PIC X(100). METHOD-ID GET-ERROR-CODE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-CODE PIC 9(4). PROCEDURE DIVISION RETURNING RETURN-CODE. MOVE ERROR-CODE TO RETURN-CODE END METHOD. METHOD-ID GET-ERROR-MESSAGE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-MESSAGE PIC X(100). PROCEDURE DIVISION RETURNING RETURN-MESSAGE. MOVE ERROR-MESSAGE TO RETURN-MESSAGE END METHOD. END EXCEPTION-OBJECT.

EXCEPTION-OBJECT is used to define exception classes with methods and properties.

Exception Object Types

Exception TypeDescriptionUse Case
SystemExceptionSystem-level exceptionsOperating system errors
FileExceptionFile operation exceptionsFile I/O errors
DataExceptionData processing exceptionsData validation errors
CustomExceptionUser-defined exceptionsApplication-specific errors
BusinessExceptionBusiness logic exceptionsBusiness rule violations

Usage in 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
* Using EXCEPTION-OBJECT in exception handling PROCEDURE DIVISION. * Try block TRY * Code that may throw exceptions PERFORM PROCESS-DATA CATCH FileException AS file-ex * Handle file exceptions DISPLAY "File error: " file-ex:GET-ERROR-MESSAGE CATCH DataException AS data-ex * Handle data exceptions DISPLAY "Data error: " data-ex:GET-ERROR-CODE CATCH SystemException AS sys-ex * Handle system exceptions DISPLAY "System error: " sys-ex:GET-ERROR-MESSAGE END-TRY * Exception object instantiation PROCEDURE DIVISION. * Create exception object INVOKE CustomException "new" RETURNING exception-obj * Set exception properties INVOKE exception-obj "SET-ERROR-CODE" USING 1001 INVOKE exception-obj "SET-ERROR-MESSAGE" USING "Custom error occurred" * Throw the exception RAISE exception-obj

EXCEPTION-OBJECT is used within TRY-CATCH blocks for structured exception handling.

Common Use Cases

EXCEPTION-OBJECT is commonly used in specific scenarios where structured exception handling and error management is needed.

File Operation 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
* File operation exception handling with EXCEPTION-OBJECT EXCEPTION-OBJECT FileOperationException. DATA DIVISION. WORKING-STORAGE SECTION. 01 FILE-NAME PIC X(50). 01 OPERATION-TYPE PIC X(10). 01 ERROR-DETAILS PIC X(200). METHOD-ID SET-FILE-INFO. DATA DIVISION. LINKAGE SECTION. 01 FILE-NAME-PARAM PIC X(50). 01 OPERATION-PARAM PIC X(10). PROCEDURE DIVISION USING FILE-NAME-PARAM OPERATION-PARAM. MOVE FILE-NAME-PARAM TO FILE-NAME MOVE OPERATION-PARAM TO OPERATION-TYPE END METHOD. METHOD-ID GET-ERROR-SUMMARY. DATA DIVISION. LINKAGE SECTION. 01 SUMMARY PIC X(100). PROCEDURE DIVISION RETURNING SUMMARY. STRING "File: " FILE-NAME " Operation: " OPERATION-TYPE " Error: " ERROR-DETAILS DELIMITED BY SIZE INTO SUMMARY END METHOD. END EXCEPTION-OBJECT. * Usage in file operations PROCEDURE DIVISION. TRY OPEN INPUT data-file READ data-file CLOSE data-file CATCH FileOperationException AS file-ex INVOKE file-ex "SET-FILE-INFO" USING "data-file" "READ" DISPLAY "File operation failed: " file-ex:GET-ERROR-SUMMARY END-TRY

Define specific exception types for different file operations.

Data Validation 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
45
46
* Data validation exception handling with EXCEPTION-OBJECT EXCEPTION-OBJECT DataValidationException. DATA DIVISION. WORKING-STORAGE SECTION. 01 FIELD-NAME PIC X(30). 01 EXPECTED-VALUE PIC X(50). 01 ACTUAL-VALUE PIC X(50). 01 VALIDATION-RULE PIC X(100). METHOD-ID SET-VALIDATION-INFO. DATA DIVISION. LINKAGE SECTION. 01 FIELD-NAME-PARAM PIC X(30). 01 EXPECTED-VAL PIC X(50). 01 ACTUAL-VAL PIC X(50). 01 RULE-PARAM PIC X(100). PROCEDURE DIVISION USING FIELD-NAME-PARAM EXPECTED-VAL ACTUAL-VAL RULE-PARAM. MOVE FIELD-NAME-PARAM TO FIELD-NAME MOVE EXPECTED-VAL TO EXPECTED-VALUE MOVE ACTUAL-VAL TO ACTUAL-VALUE MOVE RULE-PARAM TO VALIDATION-RULE END METHOD. METHOD-ID GET-VALIDATION-REPORT. DATA DIVISION. LINKAGE SECTION. 01 REPORT PIC X(200). PROCEDURE DIVISION RETURNING REPORT. STRING "Field: " FIELD-NAME " Expected: " EXPECTED-VALUE " Actual: " ACTUAL-VALUE " Rule: " VALIDATION-RULE DELIMITED BY SIZE INTO REPORT END METHOD. END EXCEPTION-OBJECT. * Usage in data validation PROCEDURE DIVISION. TRY IF customer-age < 18 INVOKE DataValidationException "new" RETURNING validation-ex INVOKE validation-ex "SET-VALIDATION-INFO" USING "customer-age" ">= 18" customer-age "Age must be 18 or older" RAISE validation-ex END-IF CATCH DataValidationException AS val-ex DISPLAY "Validation failed: " val-ex:GET-VALIDATION-REPORT END-TRY

Create structured validation exceptions with detailed error information.

Business Logic 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
45
46
47
* Business logic exception handling with EXCEPTION-OBJECT EXCEPTION-OBJECT BusinessRuleException. DATA DIVISION. WORKING-STORAGE SECTION. 01 RULE-ID PIC X(20). 01 BUSINESS-CONTEXT PIC X(100). 01 VIOLATION-DETAILS PIC X(200). 01 SEVERITY-LEVEL PIC X(10). METHOD-ID SET-BUSINESS-INFO. DATA DIVISION. LINKAGE SECTION. 01 RULE-ID-PARAM PIC X(20). 01 CONTEXT-PARAM PIC X(100). 01 DETAILS-PARAM PIC X(200). 01 SEVERITY-PARAM PIC X(10). PROCEDURE DIVISION USING RULE-ID-PARAM CONTEXT-PARAM DETAILS-PARAM SEVERITY-PARAM. MOVE RULE-ID-PARAM TO RULE-ID MOVE CONTEXT-PARAM TO BUSINESS-CONTEXT MOVE DETAILS-PARAM TO VIOLATION-DETAILS MOVE SEVERITY-PARAM TO SEVERITY-LEVEL END METHOD. METHOD-ID GET-BUSINESS-REPORT. DATA DIVISION. LINKAGE SECTION. 01 REPORT PIC X(300). PROCEDURE DIVISION RETURNING REPORT. STRING "Rule: " RULE-ID " Context: " BUSINESS-CONTEXT " Details: " VIOLATION-DETAILS " Severity: " SEVERITY-LEVEL DELIMITED BY SIZE INTO REPORT END METHOD. END EXCEPTION-OBJECT. * Usage in business logic PROCEDURE DIVISION. TRY IF account-balance < minimum-balance INVOKE BusinessRuleException "new" RETURNING business-ex INVOKE business-ex "SET-BUSINESS-INFO" USING "MIN_BALANCE" "Account maintenance" "Balance below minimum" "HIGH" RAISE business-ex END-IF CATCH BusinessRuleException AS bus-ex DISPLAY "Business rule violation: " bus-ex:GET-BUSINESS-REPORT END-TRY

Handle business rule violations with structured exception objects.

Best Practices and Tips

Following these best practices ensures effective use of the EXCEPTION-OBJECT clause for proper exception handling.

EXCEPTION-OBJECT Design Principles

  • Create specific exception types - Define specific exception classes for different error types
  • Include relevant information - Store all relevant error information in exception objects
  • Provide useful methods - Include methods for accessing and formatting error information
  • Use inheritance appropriately - Create exception hierarchies for related error types
  • Document exception behavior - Clearly document what each exception type represents
  • Handle exceptions at appropriate levels - Catch exceptions at the right level in your program

Common Pitfalls to Avoid

PitfallProblemSolution
Generic exception handlingLoss of specific error informationCreate specific exception types for different error categories
Empty exception objectsNo useful error informationAlways populate exception objects with relevant error details
Exception swallowingErrors hidden from debuggingAlways log or handle exceptions appropriately
Overly complex exception hierarchiesDifficult to maintain and understandKeep exception hierarchies simple and logical
Performance impact of exceptionsSlow performance with frequent exceptionsUse exceptions for exceptional cases, not normal flow control

Performance Considerations

  • Exception object creation overhead - Creating exception objects has overhead
  • Exception handling stack - Exception handling involves stack unwinding
  • Memory usage - Exception objects consume memory
  • Exception frequency - Frequent exceptions can impact performance
  • Exception object size - Large exception objects use more memory
  • Exception propagation - Exception propagation can be expensive

When to Use EXCEPTION-OBJECT

Use CaseEXCEPTION-OBJECT SuitabilityReasoning
Complex error handlingExcellentPerfect for structured exception handling
Object-oriented applicationsExcellentNatural fit for object-oriented programming
Error information preservationExcellentExcellent for preserving error context
Simple error handlingPoorOverkill for simple error conditions
Performance-critical codePoorException handling overhead may be too high

EXCEPTION-OBJECT Clause Quick Reference

UsageSyntaxExample
Basic exception objectEXCEPTION-OBJECT exception-nameEXCEPTION-OBJECT MyException
Exception with methodsEXCEPTION-OBJECT exception-name with METHOD-IDEXCEPTION-OBJECT MyException with GET-ERROR-INFO method
Exception instantiationINVOKE exception-name "new"INVOKE MyException "new" RETURNING ex-obj
Exception raisingRAISE exception-objectRAISE ex-obj
Exception catchingCATCH exception-name AS variableCATCH MyException AS my-ex

Test Your Knowledge

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

  • To define data types
  • To handle and manage exception objects in object-oriented COBOL
  • To control file operations
  • To perform calculations

2. Where is the EXCEPTION-OBJECT clause typically used?

  • In DATA DIVISION
  • In exception handling sections and object definitions
  • In PROCEDURE DIVISION
  • In ENVIRONMENT DIVISION

3. What information does EXCEPTION-OBJECT provide?

  • Only error codes
  • Only error messages
  • Comprehensive exception information including error details, context, and handling methods
  • Only program flow

4. When is EXCEPTION-OBJECT most useful?

  • In simple programs
  • In complex applications requiring robust error handling and exception management
  • Only during compilation
  • Only during program termination

5. How does EXCEPTION-OBJECT relate to exception handling?

  • They are completely independent
  • EXCEPTION-OBJECT is essential for modern exception handling in object-oriented COBOL
  • EXCEPTION-OBJECT overrides other exception handling features
  • They serve the same purpose

Frequently Asked Questions