Using Return Codes from Commands

Information passes between REXX and the environment. REXX sends command strings to the environment, and the environment can return information. One piece of information that the environment passes to REXX is the return code from the command.

With each command it processes, OS/2 produces 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 with no problems, the return code is nearly always 0. If something goes wrong, the return code issued is another, nonzero number. What the number is depends on the command itself and the error encountered.

This example shows how to display a return code:

/* GETRC.CMD report */
'TYPE nosuch.fil'
say 'the return code is' RC

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

/* 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 OS/2 could not find a nonexistent file. This program does not do much more than OS/2 does, but you have the basic idea of how to capture a return code.

A system error alone does not stop the running of a REXX program. Without some provision to stop the program, in this case a trap, REXX continues running. You may have to press the Control (Ctrl)+Break keys to stop processing. REXX includes the following instructions for trapping and controlling system errors:


[Back: ADDRESS Instruction]
[Next: Subcommand Processing]