MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Message Handling

Message handling in COBOL involves displaying, formatting, and managing messages to users, including error messages, informational messages, warnings, and status updates. Effective message handling provides clear, actionable feedback, improves user experience, aids debugging, and makes programs more maintainable. This includes using DISPLAY statements, formatting messages consistently, managing message codes, handling different message types, and logging messages appropriately.

Basic Message Display

Use DISPLAY statements to output messages:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PROCEDURE DIVISION. DISPLAY-MESSAGES. *> Simple text message DISPLAY 'Processing started' *> Message with variable DISPLAY 'Customer ID: ' CUSTOMER-ID *> Error message DISPLAY 'ERROR: Invalid customer ID' *> Formatted message DISPLAY 'Transaction ' TRANSACTION-ID ' completed successfully' STOP RUN.

Message Formatting

Format messages consistently for clarity:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
WORKING-STORAGE SECTION. 01 MESSAGE-BUFFER PIC X(100). 01 ERROR-CODE PIC X(4) VALUE 'E001'. 01 ERROR-TEXT PIC X(50) VALUE 'Invalid input data'. PROCEDURE DIVISION. FORMAT-MESSAGES. *> Build formatted error message STRING 'ERROR ' DELIMITED BY SIZE ERROR-CODE DELIMITED BY SIZE ': ' DELIMITED BY SIZE ERROR-TEXT DELIMITED BY SIZE INTO MESSAGE-BUFFER END-STRING DISPLAY MESSAGE-BUFFER *> Output: ERROR E001: Invalid input data STOP RUN.

Message Code System

Use message codes for maintainability:

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
WORKING-STORAGE SECTION. 01 MESSAGE-TABLE. 05 MESSAGE-ENTRY OCCURS 10 TIMES INDEXED BY MSG-INDEX. 10 MSG-CODE PIC X(4). 10 MSG-TEXT PIC X(50). 01 MESSAGE-BUFFER PIC X(100). PROCEDURE DIVISION. INITIALIZE-MESSAGES. *> Initialize message table MOVE 'E001' TO MSG-CODE(1) MOVE 'Invalid customer ID' TO MSG-TEXT(1) MOVE 'E002' TO MSG-CODE(2) MOVE 'File not found' TO MSG-TEXT(2) MOVE 'W001' TO MSG-CODE(3) MOVE 'Low balance warning' TO MSG-TEXT(3) DISPLAY-MESSAGE-BY-CODE. *> Look up and display message by code PERFORM VARYING MSG-INDEX FROM 1 BY 1 UNTIL MSG-INDEX > 10 IF MSG-CODE(MSG-INDEX) = 'E001' STRING 'ERROR ' DELIMITED BY SIZE MSG-CODE(MSG-INDEX) DELIMITED BY SIZE ': ' DELIMITED BY SIZE MSG-TEXT(MSG-INDEX) DELIMITED BY SIZE INTO MESSAGE-BUFFER END-STRING DISPLAY MESSAGE-BUFFER EXIT PERFORM END-IF END-PERFORM STOP RUN.

Message Types

Handle different message types appropriately:

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
WORKING-STORAGE SECTION. 01 MESSAGE-TYPE PIC X. 88 ERROR-MESSAGE VALUE 'E'. 88 WARNING-MESSAGE VALUE 'W'. 88 INFO-MESSAGE VALUE 'I'. PROCEDURE DIVISION. DISPLAY-BY-TYPE. IF ERROR-MESSAGE STRING 'ERROR: ' DELIMITED BY SIZE MESSAGE-TEXT DELIMITED BY SIZE INTO MESSAGE-BUFFER END-STRING DISPLAY MESSAGE-BUFFER *> Errors may stop processing ELSE IF WARNING-MESSAGE STRING 'WARNING: ' DELIMITED BY SIZE MESSAGE-TEXT DELIMITED BY SIZE INTO MESSAGE-BUFFER END-STRING DISPLAY MESSAGE-BUFFER *> Warnings continue with caution ELSE IF INFO-MESSAGE STRING 'INFO: ' DELIMITED BY SIZE MESSAGE-TEXT DELIMITED BY SIZE INTO MESSAGE-BUFFER END-STRING DISPLAY MESSAGE-BUFFER *> Info messages provide status END-IF END-IF END-IF STOP RUN.

Error Message Best Practices

Effective error messages should:

  • Be clear and concise: Easy to understand
  • Include context: What operation failed, what data was involved
  • Provide guidance: What the user should do
  • Use consistent format: Same structure for all errors
  • Include relevant data: Show values that caused the error

Logging Messages

Log important messages to files:

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
FILE SECTION. FD LOG-FILE LABEL RECORDS ARE STANDARD. 01 LOG-RECORD PIC X(200). WORKING-STORAGE SECTION. 01 CURRENT-DATE-TIME PIC X(21). 01 LOG-MESSAGE PIC X(150). PROCEDURE DIVISION. LOG-MESSAGE. *> Get timestamp MOVE FUNCTION CURRENT-DATE TO CURRENT-DATE-TIME *> Build log entry STRING CURRENT-DATE-TIME(1:14) DELIMITED BY SIZE ' | ERROR E001 | ' DELIMITED BY SIZE 'Invalid customer ID: ' DELIMITED BY SIZE CUSTOMER-ID DELIMITED BY SIZE INTO LOG-MESSAGE END-STRING *> Write to log file MOVE LOG-MESSAGE TO LOG-RECORD WRITE LOG-RECORD STOP RUN.

Test Your Knowledge

1. What is the primary way to display messages in COBOL?

  • PRINT statement
  • DISPLAY statement
  • MESSAGE statement
  • OUTPUT statement

2. What should error messages include?

  • Just the error code
  • Error code, description, context, and guidance
  • Only the error description
  • Just context information

3. What is a message code system?

  • A way to encode messages
  • A system that assigns unique codes to messages for management
  • A way to compress messages
  • A way to encrypt messages

4. How do you build formatted messages?

  • Using FORMAT statement
  • Using STRING to combine message parts
  • Messages cannot be formatted
  • Using MOVE only

5. Why use message codes instead of hardcoded text?

  • To make code shorter
  • For easier maintenance, translation, and consistency
  • To improve performance
  • To reduce memory usage

Related Concepts

Related Pages