RPG Subprocedure Error-Handling with APIs
July 11, 2012 Ted Holt
In the May 2, 2012, edition of this august publication, I shared how Brian Rusch’s shop uses an API to forward escape messages up the call stack in CL programs. The Resend Escape Message (QMHRSNEM) API works just as well in RPG subprocedures. Here’s how it’s easily done. First, you need a prototype for QMHRSNEM, and you must define the first two parameters. D ResendEscapeMsg... D pr extpgm('QMHRSNEM') D MessageKey 4a const D ErrorCode 10i 0 const You can define the remaining parameters if you want, but make sure you mark them OPTIONS(*NOPASS). If you like, place the prototype in a copybook source member. In your subprocedure, place the “meat” under a MONITOR operation. Place the call to QHMRSNEM in the corresponding ON-ERROR group, like this: D ResendEscapeMsg... D pr extpgm('QMHRSNEM') D MessageKey 4a const D ErrorCode 10i 0 const * ==================================================================== * xxxx - description and other documentation * ==================================================================== P <xxxxxxxxxxxx> b export D pi //////////////////////// D specs for parameters *** locals //////////////////////// D specs for local data /free monitor; ... do something ... do something else on-error; ResendEscapeMsg (*blanks: *zero); endmon; return; /end-free P e Notice the two arguments supplied to ResendEscapeMessage: blanks and zeros. You’ll never need any other values. The result is that the system catches any fatal error and sends it along to the calling procedure. I like the way this works. Give it a try. RELATED STORY
|