Monitor for Specific Messages in RPG
May 2, 2007 Hey, Ted:
I have a CL program that calls another CL program and monitors for specific escape messages. No big deal. I put multiple Monitor Message (MONMSG) commands after the CALL. Suppose I want to call the second CL program from an RPG program. How do I monitor for the different errors?
OK, Ray, let’s set this up for the readers. Let’s suppose your program, which we’ll call SOMEPGM, can return escape messages MYM2101 and MYM2105. A CL caller monitors for those messages like this:
CALL PGM(SOMEPGM) PARM(&whatever) MONMSG MSGID(MYM2101) EXEC(DO) /* do something */ ENDDO MONMSG MSGID(MYM2105) EXEC(DO) /* do something else */ ENDDO
In RPG programs, use the E extender when calling the program in order to trap the error. Then check positions 40 through 46 of the program status data structure to see which error you got. Here’s the code in fixed-format syntax.
D psds sds D MsgID 40 46 D ExceptData 91 170 C call(e) 'SOMEPGM' C parm whatever C if %error C select C when MsgID = 'MYM2101' C************* do something C when MsgID = 'MYM2105' C************* do something else C endsl
Here is the same example in free-format RPG.
D psds sds D MsgID 40 46 D ExceptData 91 170 D SomePgm pr extpgm('SOMEPGM') D Whatever 1a /free callp(e) SOMEPGM (whatever); if %error; select; when MsgID = 'MYM2101'; // do something when MsgID = 'MYM2105'; // do something else endsl; endif;
In case you’re interested, the text of the error message is also in the program status data structure. You’ll find it in positions 91 through 170. You can often help the user by writing this message text to an error subfile record or to a printed report.