If you are going to be a good CL programmer, you need to know how to monitor for messages with the MONMSG command. Most CL programmers think they know how, but I've seen some poor examples. Let me discuss what I consider the important points.
The message that you monitor for must be an escape type of a message sent to your program. You can't monitor for diagnostic messages, completion messages, and so forth. If you are not sure about the message type or the program name, look in the job log or request Help on the message (the first display has the type, and the second display has the From and To programs).
A big mistake some programmers make is trying to monitor for some message that isn't an escape type of message. No error occurs on the Create CL Program (CRTCLPGM) listing because the compiler does not know the valid error conditions associated with each command.
The message type is not part of the message description; the sending program decides the type of message that is sent. It is possible that the same message ID will be sent as a diagnostic by one program and an escape by another.
Another thing that may fool you is this: you see an escape message in the job log that you want to monitor for, but it isn't sent to your program. Typically, this condition occurs when some low-level program receives the message, and does not bubble it up to your program. It must be sent to your program for the MONMSG command to work.
You can also monitor for a notify type of message and a special form of status type of message. I've never found a use for this, but maybe you have.
When you monitor, the ball is in your court. You are telling the system that it is OK to proceed even though an error has occurred. The worst programs to debug are those in which the program monitored for some error and then proceeded as if the error never occurred. Although there are legitimate cases for this type of coding, the important thing to remember is that the ball is in your court. Don't drop it.
Another big mistake I see programmers making is that they always monitor for CPF0000. There is a legitimate use for this, but most of the time programmers are too lazy to determine the specific error ID. The problem with this type of coding is that the program may work for years and then give the wrong results one day because of some unusual condition.
Many commands have a wide variety of error conditions that include such things as no object, no member, no library, not authorized, can't allocate, and damaged. 1 contains an example of what I consider to be poor coding; the program is trying to ensure that there won't be a member when the Add Physical File Member (ADDPFM) command runs later in the program (or in another program).
Many commands have a wide variety of error conditions that include such things as no object, no member, no library, not authorized, can't allocate, and damaged. Figure 1 contains an example of what I consider to be poor coding; the program is trying to ensure that there won't be a member when the Add Physical File Member (ADDPFM) command runs later in the program (or in another program).
Since you can't remove a member if it is allocated (in use), if the file or library does not exist, or if some damage condition has occurred, the program may fail someday. If the program does fail, the job log may tell you that ADDPFM failed because the member exists; however, you will have to look elsewhere in the job log to see that the Remove Member (RMVM) command failed, and the actual message may not even exist (it could have been removed).
The same problem can occur on a range of generic messages like CPF9800. When you monitor for a range of messages, you really have to be an expert to understand what might happen.
No one can afford to put in a specific monitor message identifier for every possible condition, and most of the time you can't provide a more meaningful action than the system provides. Here's my philosophy of when to monitor:
o Monitor when the program can recover. For example, the situation in which the member does not exist can be handled by adding the member.
o Monitor when the system message is not meaningful-for example, the system may know the user is not authorized to the program, so it sends a message such as Not authorized to ORD500CZ in library OEDLIB. The user reading the job log has to translate this into something meaningful to the application. If this is an error you expect, it's probably worthwhile to monitor for the error and send your own specific text.
o Monitor when you are processing multiple requests and one fails. You might want to process all you can and then tell the user that the function was not 100 percent successful. I use this frequently, and it is a legitimate case for monitoring for CPF0000 on an individual command. Most of my code would look like the code in 2.
o Monitor when you are processing multiple requests and one fails. You might want to process all you can and then tell the user that the function was not 100 percent successful. I use this frequently, and it is a legitimate case for monitoring for CPF0000 on an individual command. Most of my code would look like the code in Figure 2.
I use several techniques to determine which message to monitor for. First, I use a short cheat sheet showing the few message IDs that I use at least 90 percent of the time. I am a big fan of the Check Object (CHKOBJ) command. I'd much rather check first than try to deal with exceptions that can occur when you try a "do something" type of command like Copy File (CPYF). This is my message ID list:
Check Object (CHKOBJ)
CPF9801 Not found
CPF9802 Not authorized
CPF9815 Member not found
Delete xxx (DLTxxx)
CPF2105 No object exists (most DLT commands)
Allocate Object (ALCOBJ)
CPF1002 Cannot allocate
Receive File (RCVF)
CPF0864 End of file
Then, if the message ID isn't on my list, I try the error condition and check the message with the Help key.
Finally, the AS/400 Programming Reference Summary V3R1 manual, which lists monitorable messages by command, can be useful to review the message IDs you might want to monitor for. Unfortunately, this book lists only the first level of description and can be confusing. I use the manual only in desperation or to review some things I may have overlooked.
When some commands send an escape message, the message text describes multiple conditions that could be wrong. This may be acceptable when you are reading the text, but when you are trying to program for a specific error condition, ambiguous conditions are bad news. That is why I like CHKOBJ. It's very clear what the escape conditions are.
When you have an unusual message ID you want to monitor for, make sure you read the second-level text to see if the message is ambiguous. Most of the ambiguous messages come from commands that "do something" (like CPYF) rather than "check something" (like CHKOBJ). If there is some ambiguity on a "do something" command, try to find a "check something" command you can use first to avoid the problem.
Some commands, such as CPYF or Submit Job (SBMJOB), send a general escape message preceded by a diagnostic that describes the specific problem. You can access the diagnostic message to determine the specific problem by using Receive Message (RCVMSG), but it isn't easy to do. I'll discuss some solutions in another article.
Most of the time, you will have only a single MONMSG command with a single message ID when you want to monitor. However, you can code for multiple error conditions using code like that shown in 3.
Most of the time, you will have only a single MONMSG command with a single message ID when you want to monitor. However, you can code for multiple error conditions using code like that shown in Figure 3.
I normally want to do different things on different error conditions, so I would use the other approach, shown in 4.
I normally want to do different things on different error conditions, so I would use the other approach, shown in Figure 4.
The CHKOBJ command sends an escape message when the object, library, or member is not found or not authorized. This makes for simple coding when something like not found is the error condition. You just follow CHKOBJ with a DO group as the example code in 5 shows.
The CHKOBJ command sends an escape message when the object, library, or member is not found or not authorized. This makes for simple coding when something like not found is the error condition. You just follow CHKOBJ with a DO group as the example code in Figure 5 shows.
However, when the object should not exist, you need some negative MONMSG logic. Normally, you wind up defining a label, as shown in the code in 6.
However, when the object should not exist, you need some negative MONMSG logic. Normally, you wind up defining a label, as shown in the code in Figure 6.
It is the ability to handle error conditions that helps make CL a very effective control language. Monitoring for messages can be a lot more complex than it looks. Keep it simple and take the time to find the specific case to monitor for.
Jim Sloan, president of Jim Sloan, Inc., is a software vendor and consultant. A retired IBMer, Sloan was a software planner on the S/38 when it began as a piece of paper. He also worked on the planning and early releases of the AS/400. In addition, Sloan wrote the TAA tools that exist in QUSRTOOL and is now the owner of the TAA Productivity Tools product. He has been a speaker at COMMON for many years.
REFERENCE
AS/400 Programming Reference Summary V3R1 (SX41-3720, CD-ROM QBKAUT00).
Programmer's Toolbox: How to Really Monitor for Messages
Figure 1: A Bad Example of Monitoring for a Message
/* Bad example */ RMVM FILE(LIB1/XXX) MBR(AAA) MONMSG MSGID(CPF0000) /* Ignore */ . . ADDPFM FILE(LIB1/XXX) MBR(AAA)
Programmer's Toolbox: How to Really Monitor for Messages
Figure 2: CL Example to Process Multiple Requests
XXXXXX /* Some command like MOVOBJ */ MONMSG MSGID(CPF0000) EXEC(DO) CHGVAR &ERRORFLAG 'X' /* Report error */ ENDDO . . /* At end of program */ IF (&ERRORFLAG *EQ 'X') DO /* Some error */ . /* Send escape message */ ENDDO /* Some error */
Programmer's Toolbox: How to Really Monitor for Messages
Figure 3: Monitoring for Multiple Messages at Once
CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801 CPF9802) EXEC(DO)
Programmer's Toolbox: How to Really Monitor for Messages
Figure 4: Monitoring for Multiple Messages Separately
CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801) EXEC(DO) /* No object */ . ENDDO /* No object */ MONMSG MSGID(CPF9802) EXEC(DO) /* Not authorized */ . ENDDO /* Not authorized */
Programmer's Toolbox: How to Really Monitor for Messages
Figure 5: Monitoring for an Object That Should Exist
CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801) EXEC(DO) /* Not found */ . . /* Handle error condition */ . ENDDO /* Not found */ /* Normal processing */
Programmer's Toolbox: How to Really Monitor for Messages
Figure 6: Monitoring for an Object That Should Not Exist
CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801) EXEC(GOTO NORMAL) /* Object exists */ . . /* Handle error condition */ . NORMAL: /* Normal processing */
LATEST COMMENTS
MC Press Online