Error handling in CL400
Error handling in CL400 is an important part of creating robust programs. You can use the MONITOR and ON-ERROR statements to detect and handle errors in your CL programs. Here is an explanation of how to use these statements, along with some code examples:
- MONITOR statement:
The MONITOR statement is used to detect errors in a block of code. If an error occurs, control is transferred to an ON-ERROR block where you can handle the error. Here is an example of how to use the MONITOR statement:
/* Example of the MONITOR statement */
MONITOR
/* Your code here */
ON-ERROR +
/* Handle the error here */
ENDMON
In this example, the MONITOR statement is used to surround a block of code. If an error occurs within the block, control is transferred to the ON-ERROR block. The ON-ERROR block can contain any code that you want to execute when an error occurs.
- ON-ERROR statement:
The ON-ERROR statement is used to handle errors that are detected by the MONITOR statement. Here is an example of how to use the ON-ERROR statement:
/* Example of the ON-ERROR statement */
MONITOR
/* Your code here */
ON-ERROR +
SNDPGMMSG MSG('An error occurred') +
TOUSR(*CURRENT)
ENDMON
In this example, the ON-ERROR block contains a SNDPGMMSG statement that sends a message to the current user indicating that an error occurred.
- Error handling code example:
Here is an example that demonstrates how to use the MONITOR and ON-ERROR statements to detect and handle errors:
PGM
MONMSG CPF0000
MONITOR
/* Your code here */
ON-ERROR +
SNDPGMMSG MSG('An error occurred') +
TOUSR(*CURRENT)
ENDMON
ENDPGM
In this example, the MONMSG statement is used to specify the default error handler for the program. If an error occurs outside the MONITOR statement, the default error handler is used. The MONITOR statement is then used to surround a block of code. If an error occurs within the block, control is transferred to the ON-ERROR block, where a message is sent to the current user.
Overall, the MONITOR and ON-ERROR statements are useful tools for detecting and handling errors in CL400 programs. By using these statements effectively, you can create more robust and reliable programs.
Leave a Comment