Wednesday, August 01, 2007

RPGLE and RPGIV ON-ERROR Use

With the advent of /FREE code in RPGLE, the ease of use of MONITOR and ON-ERROR have become more common. Our shop has made a silent shift to wrapping questionable data conversions and commands where failure is more likely.

The setup is really straightforward.

MONITOR;
myNumber = %Dec(myString);
ON-ERROR;
myNumber = 0;
ENDMON;

This is a simple example but it is really the only pattern for these commands. Some options are provided though with the ON-ERROR command.

ON-ERROR supports four types of error catching.
*PROGRAM
-Handles all program-error status codes, from 00100 to 00999

*FILE
-Handles all file-error status codes, from 01000 to 09999

*ALL
-Handles both program-error and file-error codes, from 00100 to 09999. This is the default.

* a specific error code
Example: ON-ERROR 1211
... handle file-not-open

A series of ON-ERROR commands can follow a single MONITOR command. IBM provides he following example.

C MONITOR
C READ FILE1
C IF NOT %EOF
C EVAL Line = %SUBST(Line(i) :
C %SCAN('***': Line(i)) + 1)
C ENDIF
C ON-ERROR 1211
C ... handle file-not-open
C ON-ERROR *FILE
C ... handle other file errors
C ON-ERROR 00100 : 00121
C ... handle string error and array-index error
C ON-ERROR
C ... handle all other errors
C ENDMON

Most frequently you can simply code ON-ERROR with no parameters.

A good use might be to monitor %OPEN and %CLOSE on file operations.

The MONITOR/ON-ERROR/ENDMON block can also aid in testing, allowing you to code around functions that don't work, but do compile.

Please do post comments, questions or examples. Also, consider emailing me with article suggestions at tojosa - at - gmail.com.

No comments: