Sunday, July 22, 2007

%Error in RPGIV - Free your statements

Have you ever wanted a better way to avoid hard errors in your RPG programs? Well, aside from MONITOR, you can use the (e) option on several commands.

For example:
Read(e)
SetLL(e)
Update(e)

Each case above prevents error from 'breaking' the program. But what to do now?

This is where %Error comes in. %Error, when placed immediately after any of these commands will indicate an error.

Example:

Read(e) I4111;
If %Error = 1;
ErrMsg = "Read Error";
EndIf;

I've found %Error most useful on Write and Update commands, where there is a chance the record or file is locked.

Additional commands with (e) qualifiers:
Close
Exfmt
In
Open
Read - all types
Reset
Set - all types
Unlock
Update
Write

There area few other less used ones, but these are the most common. I recommend using %Error in place of Monitor around these types of commands.

Is %Error and admission you might have a problem? Not at all. The busier and more complex the system, the more likely that a file problem, such as a lock, will be encountered.

The biggest plus to using error capture is that it allows the programmer to dictate a postive and controlled response. It avoids interrupting the user experience, and allows for backing out partial changes in case of an issue.

Please let me know if you have any other tips or commands you have questions about.
Your fellow coder,
Todd

No comments: