Thursday, April 05, 2007

MONITOR your way to safety

I recently discovered the usefulness of the MONITOR command in RPG IV. Familiar with it? Well I wasn't. A coworker pulled me up onto the bandwagon, and now I wonder how I didn't find this nifty little command sooner.

MONITOR can be used anywhere you'd like to trap a potential error in your RPG code. Think a numeric field might not contain a number? Think a value may not have been passed? Want to catch that divide by zero error that should never happen? How about the character string that doesn't translate into that number you'd like?

Well MONITOR can wrap that up for you. It's easy.


MONITOR;
myNumber = %Dec(myString:8:1);
On-Error;
myNumber = 0;
Error = 'bad string, non-numeric';
EndMon;


See, not hard at all. The real magic isn't in the MONITOR part of that arrangement but in the On-Error command. The On-Error command offers a variety of error codes and types as well to qualify it. On-Error *file handles just file related errors. You can also use a specific error code as well.

Where I've recently gotten a lot of use is for Processing Options from JDE Dreamwriters. Often a JDE Dreamwriter will contain a processing option that is intended to be read as numeric. Since humans have clumsy fingers and brains, sometimes a non-numeric value will be placed in one of those options.

Before, I'd have read that processing option in and had to test it for numeric and still put code in to move it to the number field. Now I can simply monitor and trap any error and still do nothing special if the move to numeric works.

Anyone notice the similarity to the Try-Catch block structure of Java?

More on fun RPG IV commands coming up. If you have a request for one of the new /FREE commands to be highlighted, do let me know.

Your intrepid and free programmer,
TJ




No comments: