Conditions and condition traps

A condition is a specified event or state that CALL ON or SIGNAL ON can trap. A condition trap can modify the flow of execution in a REXX program.

Condition traps are turned on or off using the ON or OFF subkeywords of the SIGNAL and CALL instructions (see CALL and SIGNAL).

Read syntax diagramSkip visual syntax diagramCALLSIGNALOFFconditionONconditionNAMEtrapname ;

condition and trapname are single symbols that are taken as constants. Following one of these instructions, a condition trap is set to either ON (enabled) or OFF (disabled). The initial setting for all condition traps is OFF.

If a condition trap is enabled and the specified condition occurs, control passes to the routine or label trapname if you have specified trapname. Otherwise, control passes to the routine or label condition. CALL or SIGNAL is used, depending on whether the most recent trap for the condition was set using CALL ON or SIGNAL ON, respectively.

Note: If you use CALL, the trapname can be an internal label, a built-in function, or an external routine. If you use SIGNAL, the trapname can be only an internal label.

The conditions and their corresponding events that can be trapped are:

ERROR
raised if a command indicates an error condition upon return. It is also raised if any command indicates failure and neither CALL ON FAILURE nor SIGNAL ON FAILURE is active. The condition is raised at the end of the clause that called the command but is ignored if the ERROR condition trap is already in the delayed state. The delayed state is the state of a condition trap when the condition has been raised but the trap has not yet been reset to the enabled (ON) or disabled (OFF) state. See Note.

CALL ON ERROR and SIGNAL ON ERROR trap all positive return codes, and negative return codes only if CALL ON FAILURE and SIGNAL ON FAILURE are not set.

FAILURE
raised if a command indicates a failure condition upon return. The condition is raised at the end of the clause that called the command but is ignored if the FAILURE condition trap is already in the delayed state.

CALL ON FAILURE and SIGNAL ON FAILURE trap all negative return codes from commands.

HALT
raised if an external attempt is made to interrupt and end execution of the program. The condition is usually raised at the end of the clause that was being processed when the external interruption occurred.
NOVALUE
raised if an uninitialized variable is used:
  • As a term in an expression
  • As the name following the VAR subkeyword of a PARSE instruction
  • As a variable reference in a parsing template, a PROCEDURE instruction, or a DROP instruction.
    Note: SIGNAL ON NOVALUE can trap any uninitialized variables except tails in compound variables.
    /* The following does not raise NOVALUE. */
    signal on novalue
    a.=0
    say a.z
    say 'NOVALUE is not raised.'
    exit
    
    novalue:
    say 'NOVALUE is raised.'

You can specify this condition only for SIGNAL ON.

SYNTAX
raised if any language processing error is detected while the program is running. This includes all kinds of processing errors, including true syntax errors and run-time errors, such as attempting an arithmetic operation on nonnumeric terms. You can specify this condition only for SIGNAL ON.

Any ON or OFF reference to a condition trap replaces the previous state (ON, OFF, or DELAY, and any trapname) of that condition trap. Thus, a CALL ON HALT replaces any current SIGNAL ON HALT (and a SIGNAL ON HALT replaces any current CALL ON HALT), a CALL ON or SIGNAL ON with a new trap name replaces any previous trap name, any OFF reference disables the trap for CALL or SIGNAL, and so on.

Note: The state (ON, OFF, or DELAY, and any trapname) of each condition trap is saved on entry to a subroutine and is then restored on RETURN. This means that CALL ON, CALL OFF, SIGNAL ON, and SIGNAL OFF can be used in a subroutine without affecting the conditions set up by the caller. See the CALL instruction (CALL) for details of other information that is saved during a subroutine call.