Rexx - Error Handling



Rexx has the ability to also work on Error handling as in other programming languages.

The following are some of the various error conditions that are seen in Rexx.

  • ERROR − This even is raised whenever a command which is sent to the operating system results in an error.

  • FAILURE − This even is raised whenever a command which is sent to the operating system results in a failure.

  • HALT − This is normally raised whenever an operation is dependent on another operation. An example is if an I/O operation is being halted for any reason.

  • NOVALUE − This event is raised when a value has not been assigned to a variable.

  • NOTREADY − This is raised by any I/O device which is not ready to accept any operation.

  • SYNTAX − This event is raised if there is any syntax error in the code.

  • LOSTDIGITS − This event is raised when an arithmetic operation results in a loss of digits during the operation.

Trapping Errors

Errors are trapped with the help of the signal command. Let’s take a look at the syntax and an example of this.

Syntax

signal on [Errorcondition]

Where,

  • Errorcondition − This is the error condition which is given above.

Example

Let’s take a look at an example on this.

/* Main program */ 
signal on error 
signal on failure 
signal on syntax 
signal on novalue beep(1) 
signal off error 
signal off failure
signal off syntax 
signal off novalue 
exit 0 
error: failure: syntax: novalue: 
say 'An error has occured'

In the above example, we first turn the error signals on. We then add a statement which will result in an error. We then have the error trap label to display a custom error message.

The output of the above program will be as shown below.

An error has occurred.

An example of error codes is shown in the following program.

/* Main program */ 
signal on error 
signal on failure 
signal on syntax 
signal on novalue beep(1) 
exit 0 
error: failure: syntax: novalue: 

say 'An error has occured' 
say rc 
say signal 

The output of the above program will be as shown below.

An error has occured 
40 
6
Advertisements