MainframeMaster

COBOL Tutorial

COBOL MESSAGE Clause - Quick Reference

Progress0 of 0 lessons

Overview

The MESSAGE clause is used in COBOL to define message text that can be used for communication, error handling, and user interface purposes. It provides a structured way to manage and maintain message content throughout COBOL applications.

Purpose and Usage

  • Message definition - Define reusable message text
  • Error handling - Provide consistent error messages
  • User communication - Display information to users
  • Maintainability - Centralize message management
  • Internationalization - Support multiple languages

Message Management Concept

Define messages in WORKING-STORAGE SECTION
Reference messages by data item name
Use DISPLAY to show messages to users
Centralize message management
Support multiple languages and formats

The MESSAGE clause provides structured message management in COBOL programs.

Syntax and Usage

The MESSAGE clause is used to define message text in the DATA DIVISION. It provides a structured approach to message management in COBOL programs.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
* Basic MESSAGE clause syntax level-number data-name PIC X(length) VALUE "message text". * Examples 01 ERROR-MESSAGE-001 PIC X(50) VALUE "File not found". 01 SUCCESS-MESSAGE PIC X(40) VALUE "Operation completed successfully". 01 WARNING-MESSAGE PIC X(60) VALUE "Warning: Data may be incomplete". * Complete example WORKING-STORAGE SECTION. 01 MESSAGES. 05 MSG-FILE-NOT-FOUND PIC X(50) VALUE "Error: File not found". 05 MSG-INVALID-INPUT PIC X(45) VALUE "Error: Invalid input data". 05 MSG-SUCCESS PIC X(40) VALUE "Operation completed successfully". 05 MSG-WARNING PIC X(60) VALUE "Warning: Please check your input".

Messages are defined using the VALUE clause with PIC X data types.

Complete Example

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
48
49
50
51
* Complete example using MESSAGE clause IDENTIFICATION DIVISION. PROGRAM-ID. MESSAGE-DEMO. ENVIRONMENT DIVISION. CONFIGURATION SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 MESSAGE-TABLE. 05 MSG-ERROR-001 PIC X(50) VALUE "Error: File not found". 05 MSG-ERROR-002 PIC X(45) VALUE "Error: Invalid input data". 05 MSG-ERROR-003 PIC X(55) VALUE "Error: Insufficient permissions". 05 MSG-SUCCESS-001 PIC X(40) VALUE "Operation completed successfully". 05 MSG-WARNING-001 PIC X(60) VALUE "Warning: Data may be incomplete". 05 MSG-INFO-001 PIC X(35) VALUE "Processing data...". 01 USER-INPUT PIC X(20). 01 FILE-STATUS PIC XX. 01 ERROR-CODE PIC 9(3). PROCEDURE DIVISION. MAIN-LOGIC. * Get user input DISPLAY MSG-INFO-001 ACCEPT USER-INPUT * Validate input IF USER-INPUT = SPACES DISPLAY MSG-ERROR-002 STOP RUN END-IF * Process file operation PERFORM PROCESS-FILE * Display success message DISPLAY MSG-SUCCESS-001 STOP RUN. PROCESS-FILE. * Simulate file processing IF FILE-STATUS = "35" DISPLAY MSG-ERROR-001 MOVE 1 TO ERROR-CODE ELSE IF FILE-STATUS = "37" DISPLAY MSG-ERROR-003 MOVE 3 TO ERROR-CODE ELSE DISPLAY MSG-WARNING-001 END-IF.

This example shows how to define and use messages throughout a program.

Error Handling Example

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
* Error handling with MESSAGE clause IDENTIFICATION DIVISION. PROGRAM-ID. ERROR-HANDLER. DATA DIVISION. WORKING-STORAGE SECTION. 01 ERROR-MESSAGES. 05 ERR-FILE-NOT-FOUND PIC X(50) VALUE "Error: File not found". 05 ERR-INVALID-DATA PIC X(45) VALUE "Error: Invalid data format". 05 ERR-PERMISSION PIC X(55) VALUE "Error: Insufficient permissions". 05 ERR-SYSTEM PIC X(40) VALUE "Error: System error occurred". 01 FILE-STATUS PIC XX. 01 DATA-VALIDATION PIC X(1). 01 ERROR-TYPE PIC 9(1). PROCEDURE DIVISION. MAIN-LOGIC. * Perform operations with error handling PERFORM VALIDATE-DATA PERFORM PROCESS-FILE PERFORM CHECK-PERMISSIONS STOP RUN. VALIDATE-DATA. * Validate input data IF DATA-VALIDATION = "N" DISPLAY ERR-INVALID-DATA MOVE 2 TO ERROR-TYPE END-IF. PROCESS-FILE. * Process file with error handling IF FILE-STATUS = "35" DISPLAY ERR-FILE-NOT-FOUND MOVE 1 TO ERROR-TYPE ELSE IF FILE-STATUS = "37" DISPLAY ERR-PERMISSION MOVE 3 TO ERROR-TYPE ELSE IF FILE-STATUS NOT = "00" DISPLAY ERR-SYSTEM MOVE 4 TO ERROR-TYPE END-IF.

Error handling uses predefined messages for consistent error reporting.

Common Use Cases

The MESSAGE clause is essential in various scenarios where structured message management is critical for user communication and error handling.

User Interface Applications

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
48
49
50
51
52
53
54
55
56
* User interface with MESSAGE clause IDENTIFICATION DIVISION. PROGRAM-ID. USER-INTERFACE. DATA DIVISION. WORKING-STORAGE SECTION. 01 UI-MESSAGES. 05 MSG-WELCOME PIC X(40) VALUE "Welcome to the application". 05 MSG-MENU-OPTION-1 PIC X(30) VALUE "1. Process Data". 05 MSG-MENU-OPTION-2 PIC X(30) VALUE "2. Generate Report". 05 MSG-MENU-OPTION-3 PIC X(30) VALUE "3. Exit". 05 MSG-ENTER-CHOICE PIC X(25) VALUE "Enter your choice: ". 05 MSG-INVALID-CHOICE PIC X(35) VALUE "Invalid choice. Please try again.". 05 MSG-PROCESSING PIC X(25) VALUE "Processing...". 05 MSG-COMPLETE PIC X(30) VALUE "Operation completed.". 01 USER-CHOICE PIC X(1). 01 PROCESSING-STATUS PIC X(1). PROCEDURE DIVISION. MAIN-LOGIC. * Display welcome message DISPLAY MSG-WELCOME * Display menu DISPLAY MSG-MENU-OPTION-1 DISPLAY MSG-MENU-OPTION-2 DISPLAY MSG-MENU-OPTION-3 * Get user choice DISPLAY MSG-ENTER-CHOICE ACCEPT USER-CHOICE * Process choice EVALUATE USER-CHOICE WHEN "1" DISPLAY MSG-PROCESSING PERFORM PROCESS-DATA DISPLAY MSG-COMPLETE WHEN "2" DISPLAY MSG-PROCESSING PERFORM GENERATE-REPORT DISPLAY MSG-COMPLETE WHEN "3" STOP RUN WHEN OTHER DISPLAY MSG-INVALID-CHOICE END-EVALUATE. PROCESS-DATA. * Process data logic here MOVE "Y" TO PROCESSING-STATUS. GENERATE-REPORT. * Generate report logic here MOVE "Y" TO PROCESSING-STATUS.

User interface applications use messages for consistent user communication.

Logging and Debugging

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
48
49
50
51
52
53
54
* Logging and debugging with MESSAGE clause IDENTIFICATION DIVISION. PROGRAM-ID. LOGGING-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 LOG-MESSAGES. 05 LOG-START PIC X(30) VALUE "Program execution started". 05 LOG-DATA-LOADED PIC X(35) VALUE "Data loaded successfully". 05 LOG-PROCESSING PIC X(30) VALUE "Processing data...". 05 LOG-VALIDATION PIC X(30) VALUE "Data validation completed". 05 LOG-ERROR PIC X(25) VALUE "Error occurred: ". 05 LOG-COMPLETE PIC X(30) VALUE "Program execution completed". 01 LOG-FILE-NAME PIC X(20) VALUE "PROGRAM.LOG". 01 ERROR-DETAILS PIC X(50). 01 LOG-ENTRY PIC X(100). PROCEDURE DIVISION. MAIN-LOGIC. * Log program start PERFORM WRITE-LOG-ENTRY USING LOG-START * Load data PERFORM LOAD-DATA PERFORM WRITE-LOG-ENTRY USING LOG-DATA-LOADED * Process data PERFORM WRITE-LOG-ENTRY USING LOG-PROCESSING PERFORM PROCESS-DATA * Validate data PERFORM WRITE-LOG-ENTRY USING LOG-VALIDATION PERFORM VALIDATE-DATA * Log completion PERFORM WRITE-LOG-ENTRY USING LOG-COMPLETE STOP RUN. WRITE-LOG-ENTRY USING LOG-TEXT. * Write log entry to file MOVE FUNCTION CURRENT-DATE TO LOG-ENTRY(1:8) MOVE " - " TO LOG-ENTRY(9:3) MOVE LOG-TEXT TO LOG-ENTRY(12:50) * Write LOG-ENTRY to log file here. LOAD-DATA. * Load data logic here. PROCESS-DATA. * Process data logic here. VALIDATE-DATA. * Validate data logic here.

Logging and debugging use messages for consistent log entries.

Internationalization

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
* Internationalization with MESSAGE clause IDENTIFICATION DIVISION. PROGRAM-ID. I18N-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 LANGUAGE-SETTING PIC X(2) VALUE "EN". 01 ENGLISH-MESSAGES. 05 EN-HELLO PIC X(20) VALUE "Hello, World!". 05 EN-GOODBYE PIC X(20) VALUE "Goodbye!". 05 EN-ERROR PIC X(30) VALUE "An error occurred.". 01 SPANISH-MESSAGES. 05 ES-HELLO PIC X(20) VALUE "¡Hola, Mundo!". 05 ES-GOODBYE PIC X(20) VALUE "¡Adiós!". 05 ES-ERROR PIC X(30) VALUE "Ocurrió un error.". 01 FRENCH-MESSAGES. 05 FR-HELLO PIC X(20) VALUE "Bonjour, Monde!". 05 FR-GOODBYE PIC X(20) VALUE "Au revoir!". 05 FR-ERROR PIC X(30) VALUE "Une erreur s'est produite.". 01 CURRENT-MESSAGE PIC X(30). PROCEDURE DIVISION. MAIN-LOGIC. * Display message based on language setting PERFORM DISPLAY-MESSAGE USING "HELLO" PERFORM DISPLAY-MESSAGE USING "GOODBYE" PERFORM DISPLAY-MESSAGE USING "ERROR" STOP RUN. DISPLAY-MESSAGE USING MESSAGE-TYPE. * Select message based on language and type EVALUATE LANGUAGE-SETTING WHEN "EN" EVALUATE MESSAGE-TYPE WHEN "HELLO" MOVE EN-HELLO TO CURRENT-MESSAGE WHEN "GOODBYE" MOVE EN-GOODBYE TO CURRENT-MESSAGE WHEN "ERROR" MOVE EN-ERROR TO CURRENT-MESSAGE END-EVALUATE WHEN "ES" EVALUATE MESSAGE-TYPE WHEN "HELLO" MOVE ES-HELLO TO CURRENT-MESSAGE WHEN "GOODBYE" MOVE ES-GOODBYE TO CURRENT-MESSAGE WHEN "ERROR" MOVE ES-ERROR TO CURRENT-MESSAGE END-EVALUATE WHEN "FR" EVALUATE MESSAGE-TYPE WHEN "HELLO" MOVE FR-HELLO TO CURRENT-MESSAGE WHEN "GOODBYE" MOVE FR-GOODBYE TO CURRENT-MESSAGE WHEN "ERROR" MOVE FR-ERROR TO CURRENT-MESSAGE END-EVALUATE END-EVALUATE DISPLAY CURRENT-MESSAGE.

Internationalization uses message sets for different languages.

Best Practices and Tips

Following these best practices ensures effective use of the MESSAGE clause in COBOL applications.

MESSAGE Clause Best Practices

  • Use descriptive names - Name message data items clearly and consistently
  • Group related messages - Organize messages by type or function
  • Include message codes - Add error codes for easier maintenance
  • Use consistent formatting - Maintain consistent message style and length
  • Plan for internationalization - Consider multiple language support
  • Document message usage - Document when and how messages are used

Message Organization Patterns

PatternDescriptionExample
By TypeGroup by message typeERROR-MESSAGES, INFO-MESSAGES
By FunctionGroup by program functionFILE-MESSAGES, VALIDATION-MESSAGES
By LanguageGroup by languageENGLISH-MESSAGES, SPANISH-MESSAGES
By ModuleGroup by program moduleMAIN-MESSAGES, UTIL-MESSAGES
By SeverityGroup by error severityFATAL-MESSAGES, WARNING-MESSAGES

Performance Considerations

  • Memory usage - Messages consume memory based on their length
  • Initialization overhead - Large message tables may impact startup time
  • String operations - Message manipulation may affect performance
  • I/O operations - Displaying messages involves I/O overhead
  • Maintenance - Large message sets require careful maintenance
  • Testing - Test message display with various scenarios

When to Use MESSAGE Clause

Use CaseMESSAGE SuitabilityReasoning
Error handlingExcellentProvides consistent error messaging
User interfacesExcellentCentralizes user communication
LoggingGoodProvides structured log messages
InternationalizationGoodSupports multiple languages
Data processingPoorNot designed for data manipulation

MESSAGE Clause Quick Reference

UsageSyntaxExample
Basic message01 data-name PIC X(length) VALUE "text"01 MSG-ERROR PIC X(30) VALUE "Error occurred"
Message group01 group-name. 05 msg1 PIC X(n) VALUE "text"01 MESSAGES. 05 MSG-1 PIC X(20) VALUE "Hello"
Display messageDISPLAY message-nameDISPLAY MSG-ERROR
Error message01 error-msg PIC X(n) VALUE "Error: description"01 ERR-FILE PIC X(40) VALUE "Error: File not found"
Success message01 success-msg PIC X(n) VALUE "Success message"01 MSG-SUCCESS PIC X(35) VALUE "Operation completed"

Test Your Knowledge

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

  • To perform arithmetic operations
  • To define message text and handle communication
  • To control file operations
  • To manage memory allocation

2. Where is the MESSAGE clause typically defined in a COBOL program?

  • In the PROCEDURE DIVISION
  • In the DATA DIVISION under WORKING-STORAGE SECTION
  • In the ENVIRONMENT DIVISION
  • In the IDENTIFICATION DIVISION

3. How is the MESSAGE clause typically used in COBOL programs?

  • For arithmetic calculations
  • For defining error messages and user communication
  • For file I/O operations
  • For data validation

4. Which of the following is a common use case for the MESSAGE clause?

  • Data processing
  • Error handling and user feedback
  • File operations
  • Mathematical calculations

5. How do you reference a message defined with the MESSAGE clause?

  • By using the message text directly
  • By referencing the data item name
  • By calling a system function
  • By using a special register

Frequently Asked Questions