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.
Use DISPLAY statements to output messages:
123456789101112131415PROCEDURE 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.
Format messages consistently for clarity:
12345678910111213141516171819WORKING-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.
Use message codes for maintainability:
1234567891011121314151617181920212223242526272829303132333435363738WORKING-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.
Handle different message types appropriately:
123456789101112131415161718192021222324252627282930313233343536WORKING-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.
Effective error messages should:
Log important messages to files:
123456789101112131415161718192021222324252627FILE 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.
1. What is the primary way to display messages in COBOL?
2. What should error messages include?
3. What is a message code system?
4. How do you build formatted messages?
5. Why use message codes instead of hardcoded text?