Exit in COBOL
Here’s an idiomatic COBOL example demonstrating the concept of exiting a program with a specific status code:
This COBOL program demonstrates how to exit a program with a specific status code. Here’s an explanation of the code:
We define a variable
WS-EXIT-CODE
in the WORKING-STORAGE SECTION to hold our exit code value (3 in this case).In the PROCEDURE DIVISION, we use the
STOP RUN RETURNING
statement to exit the program with the specified exit code.Any code after the
STOP RUN
statement will not be executed, similar to theos.Exit()
function in the original example.
To compile and run this COBOL program:
Save the code in a file named
exit-example.cob
.Compile the program using your COBOL compiler. For example, with GnuCOBOL:
Run the compiled program:
Check the exit code:
Note that the exact method to check the exit code may vary depending on your operating system and shell.
In COBOL, unlike some other languages, there isn’t a direct equivalent to deferred functions or cleanup operations that are guaranteed to run on program exit. The program will simply terminate when it reaches the STOP RUN
statement.
This example shows how COBOL handles program termination and exit codes, which is conceptually similar to the original example, although the syntax and specifics differ due to the nature of COBOL as a language.