Errors in COBOL

In COBOL, error handling is typically done using condition names and the ON EXCEPTION clause. We’ll demonstrate a similar concept to Go’s error handling using these COBOL features.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ERROR-HANDLING-EXAMPLE.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-RESULT             PIC S9(9) COMP.
       01 WS-ERROR-MESSAGE      PIC X(50).
       01 WS-LOOP-COUNTER       PIC 9(2).

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           PERFORM VARYING WS-LOOP-COUNTER FROM 1 BY 1 
               UNTIL WS-LOOP-COUNTER > 2
               MOVE FUNCTION RANDOM * 100 TO WS-RESULT
               PERFORM PROCESS-RESULT
           END-PERFORM

           PERFORM VARYING WS-LOOP-COUNTER FROM 1 BY 1 
               UNTIL WS-LOOP-COUNTER > 5
               PERFORM MAKE-TEA
           END-PERFORM

           STOP RUN.

       PROCESS-RESULT.
           IF WS-RESULT = 42
               MOVE "Can't work with 42" TO WS-ERROR-MESSAGE
               PERFORM DISPLAY-ERROR
           ELSE
               ADD 3 TO WS-RESULT
               DISPLAY "Result: " WS-RESULT
           END-IF.

       MAKE-TEA.
           EVALUATE WS-LOOP-COUNTER
               WHEN 2
                   MOVE "No more tea available" TO WS-ERROR-MESSAGE
                   PERFORM DISPLAY-ERROR
               WHEN 4
                   MOVE "Can't boil water" TO WS-ERROR-MESSAGE
                   STRING "Making tea: " DELIMITED BY SIZE
                          WS-ERROR-MESSAGE DELIMITED BY SIZE
                          INTO WS-ERROR-MESSAGE
                   PERFORM DISPLAY-ERROR
               WHEN OTHER
                   DISPLAY "Tea is ready!"
           END-EVALUATE.

       DISPLAY-ERROR.
           DISPLAY "Error: " WS-ERROR-MESSAGE.

In this COBOL program, we’ve implemented a similar structure to the Go example, adapting it to COBOL’s syntax and conventions.

  1. We define variables in the WORKING-STORAGE SECTION to hold our result, error messages, and a loop counter.

  2. The MAIN-PROCEDURE contains two loops that correspond to the loops in the Go example.

  3. The PROCESS-RESULT paragraph is similar to the f function in Go. It checks if the result is 42 and sets an error message if so. Otherwise, it adds 3 to the result.

  4. The MAKE-TEA paragraph corresponds to the makeTea function in Go. It uses the EVALUATE statement (similar to a switch statement) to handle different cases.

  5. The DISPLAY-ERROR paragraph is used to display error messages, similar to how errors are printed in the Go example.

In COBOL, we don’t have a built-in error type or a way to return multiple values from a procedure. Instead, we use condition handling and global variables to communicate errors.

The concept of wrapping errors is simulated by concatenating strings in the MAKE-TEA paragraph when WS-LOOP-COUNTER is 4.

While COBOL doesn’t have an exact equivalent to Go’s errors.Is function, we can achieve similar functionality by comparing error messages directly.

This COBOL program demonstrates error handling patterns that are idiomatic to COBOL while maintaining a structure similar to the original Go example.