Using Return Codes from Commands

With each command it processes, Windows, OS/2 and Unix/Linux command shells produce a number called a return code. When a Rexx program is running, this return code is automatically assigned to a special built-in Rexx variable named RC.

If the command was processed without problems, the return code is almost always 0. If something goes wrong, the return code issued is a nonzero number. The number depends on the command itself and the error encountered.

This example shows how to display a return code:


getrc&per;cmd (Windows)
/* getrc&per;cmd report */
"TYPE nosuch.fil"
say "the return code is" RC

The special variable RC can be used in expressions like any other variable. In the next example, an error message is displayed when the TYPE command returns a nonzero value in RC:


RC special variable
/* Simple if/then error handler */
say "Type a file name:"
pull filename
"TYPE" filename
if RC \\= 0
then say "Could not find" filename

This program tells you only that the system could not find a nonexistent file.

A system error does not stop a Rexx program. Without some provision to stop the program, in this case a trap, Rexx continues running. You might have to press the Ctrl+Break key combination to stop processing. Rexx includes the following instructions for trapping and controlling system errors: