RPG MONITOR is a Flexible Facility

The more classes we teach on the new V5 features of RPG IV, the more we like these recent improvements. A particular favorite is MONITOR support. Have you ever wanted to be able to catch those pesky decimal data errors and programmatically do something useful with them? How about numeric overflow or string errors? Well, now you can.

As much as we liked this new facility when we first saw it, it wasn't until recently that we began to realize that it was changing our programming style. We think it should change yours too -- and, once you read this article, hopefully you'll agree and consider adapting your own programming style to take advantage of the new support. (Note: Those familiar with Java will notice that some aspects of MONITOR support are similar to Java's try/catch mechanism.)

Let's begin by studying the basics of using the MONITOR op-code. The example below illustrates the basic "shape" of what's known as a "Monitor group."

<1> C                  MONITOR
     // Code required processing task here
<2> C                   ON-ERROR xxxxx : yyyyy : zzzzz
     // Add code here to process error codes xxxxx, yyyyy and zzzzz
<3> C                   ON-ERROR *FILE
    // Process any file related errors not caught above
<4> C                   ON-ERROR *ALL
    // Process any remaining errors
<5> C                   ENDMON

The MONITOR op-code <1> identifies the beginning of the group. It's followed by the operations that are to be monitored, which are referred to collectively as the "monitor block." This block is followed by one (or more) ON-ERROR op-codes, which determine the type of error to be processed by the error block that follows. This is similar to the WHEN op-code in a SELECT group in that it introduces a group of operations that are to be performed when a specific condition is encountered.

The first ON-ERROR <2> illustrates the ability to monitor for specific error conditions. Specify one or more status code values as shown. The values to be used are those that are reported by %STATUS (and in the *STATUS area of the Program Status Data Structure). You'll find them detailed in the RPG Reference manual in the chapter on File and Program Exception/Errors. We have included a few examples in Figure 1.

If any of the listed exceptions are signaled, control is transferred to the code in the error block. When that code completes, control transfers to the logic following the ENDMON. If there are other specific errors that we wish to trap, we can code an additional ON-ERROR together with its associated error block. (Note: While exception codes can be coded as simple numeric literals, if you want future generations of programmers to understand what you're trying to achieve, it's better to use named constants, an example of which is outlined below.)

















Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

comments powered by Disqus



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters