Pages

Wednesday, January 25, 2023

Finding a way to monitor for more than one message with RPG's monitor

Perhaps this title is a little misleading, I struggled to come up with something that adequately describe this scenario in the space allowed.

I was asked if there was an easy way when using a Monitor group to perform some kind of action if the message id starts with something like 'RNX12'.

Let me get start with my example RPG code:

01  **free

02  dcl-ds PgmDS qualified psds ;
03    ErrorId char(7) pos(40) ;
04    ShortErrorId char(5) pos(40) ;
05  end-ds ;

06  dcl-f TESTFILE usropn ;

07  open TESTFILE ;
08  close TESTFILE ;

Line 1: Yes, this program is in totally free RPG

Lines 2 – 5: This is the definition of a data structure that is the RPG program data structure. As I only need two subfields from the data structure I have just given them here. Line 3 is for the complete error message id, and line 4 is only for the first five characters of it.

Line 6: I need to define a file, that is user open. Its layout and contents are irrelevant.

Line 7: I open the file.

Line 8: I close the file.

The first possible solution I came up with is:

09  monitor ;
10   read TESTFILE ;

11  on-excp 'RNX1200' : 'RNX1201' : 'RNX1202' :
             ...       
12          'RNX1297' : 'RNX1298' : 'RNX1299' ;
       // Do something
13  endmon ;

Lines 9 – 13: This is the first example Monitor group.

Line 10: A read is performed to TESTFILE which is closed, therefore, an error occurs.

Lines 11 and 12: The documentation for the ON-EXCP operation code states that you can give multiple error ids, each separated by a colon ( : ). But it does not state how many. Personally I doubt it would compile if I entered 99 errors ids. In reality for any large number of error ids it would be a lot of work to enter them all. There must be an easier way.

The second solution, if used would replace the first solution's monitor group code.

09  monitor ;
10   read TESTFILE ;

11  on-error ;
12    if (PgmDS.ShortErrorId = 'RNX12') ;
        // Do something else
13    endif ;
14  endmon ;

Line 9 and 10: Same as above.

Line 11: The code following the ON-ERROR operation will be executed no matter what error occurs.

Line 12: I can now check if the "short" error id, defined on line 4, is what I want it to be, 'RNX12'.

Simple, easy, and a lot less code than using the ON-EXCP operation with all the error ids listed after it.

 

This article was written for IBM i 7.5, and should work for some earlier releases too.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.