Basic Error Handling

Transcription

Basic Error Handling
Basic Error Handling
Part 12
Presentation © Copyright 2008, Bryan Meyers, www.bmeyers.net
Types of Errors
• Program logic errors
• Anticipated errors
– Check to see if a file exists
– Check for an object lock
• Unexpected errors
– Divide by zero
– Program not found
• System errors
– Power failure
– DASD failure
Error Messages
• Program sends *ESCAPE message if error
occurs while executing a CL command
• If program has no error handling mechanism
in place, it sends an inquiry (*INQ) message
for response
– To user for interactive jobs
– To system operator for batch jobs
Error Messages
• Job log shows messages
– DSPJOBLOG command
Escape message
Inquiry message
Reply
Diagnostic message
Error Messages
• Press F1 to see additional information about a
message
Message Files
• *MSGF object stores message descriptions
– QCPFMSG is operating system message file
– View contents with WRKMSGD command
– CRTMSGF command will create a message file for
application messages
• ADDMSGD command adds messages to a message file
• Three parts to message
– Message identifier
• Prefix: E.g., CPF, MCH, RNQ, CBL
• Message number (hex)
– Message text
– Message data
Message Files
• WRKMSGD command displays message details
CHKOBJ Command
• CHKOBJ determines whether an object exists
– Object existence test (OBJ, OBJTYPE)
– File member existence test (MBR)
– Object authority test (AUT)
• Sends escape message if test fails
–
–
–
–
–
–
–
CPF9801 Object not found
CPF9802 Not authorized to object
CPF9810 Library not found
CPF9815 Member not found
CPF9820 Not authorized to use library
CPF9830 Cannot assign library
CPF9899 Error occurred during processing of command
CHKOBJ Command
MONMSG Command
• Monitors program for occurrence of *ESCAPE message
– Specifies action to take when message appears
– May prevent *INQ message
• Three parameters
– MSGID identifies message(s) to monitor
• Up to 50 messages may be specified
– CMPDTA allows comparison with specific message data
• Rarely used
– EXEC specifies single command to execute when message
appears
• If no command is specified, message is ignored
MONMSG MSGID(CPF9801) EXEC(RETURN)
MONMSG CPF9801 EXEC(RETURN)
Command-Level MONMSG
• Appears immediately following a command
• MONMSG applies only to single command
Applies only to second CHKOBJ
PGM
CHKOBJ Mylib/Myfile *FILE
CHKOBJ Mylib/Myfile2 *FILE
MONMSG CPF9801 EXEC(CRTPF Mylib/Myfile2 RCDLEN(80))
CALL Mypgm
RETURN
ENDPGM
Program-Level MONMSG
• Appears as first command in procedure
section of program
• MONMSG applies to every command in
program
– Unless command level MONMSG affects
command
– Usually monitors for generic messages
• Also called global message monitor
• Can only execute GOTO command
Program-Level MONMSG
Applies to all commands
PGM
DCL …
DCLF …
MONMSG CPF9801 EXEC(GOTO End)
CHKOBJ MyLib/MyFile *FILE MBR(MyMbr1) AUT(*ALL)
CHKOBJ MyLib/MyFile *FILE MBR(MyMbr2) AUT(*ALL)
CALL MyPgm
End:
RETURN
ENDPGM
Mixed-Level MONMSGs
• MONMSG can appear at both program level
and command level
– Command level is considered first
PGM
MONMSG CPF0000 EXEC(GOTO ERRORS)
ADDLIBLE Mylib
MONMSG CPF2103
/* Already in list
*/
CHKOBJ Myfile *FILE
MONMSG CPF9801 EXEC(CRTPF Mylib/Myfile RCDLEN(80))
CALL Mypgm
RETURN
ERRORS:
DMPCLPGM
/* Dump the CL program */
MONMSG CPF0000
/* Just in case
*/
SNDPGMMSG MSG(‘Error occurred in program.’)
RETURN
ENDPGM
Generic Message Monitor
• MONMSG can specify up to 50 messages
• Message identifier use 00 or 0000 to specify a
message range
CHKOBJ Myfile *FILE MBR(Mymbr) AUT(*ALL)
MONMSG (CPF9801 CPF9802) EXEC(GOTO ERRORS)
CHKOBJ Myfile *FILE MBR(Mymbr) AUT(*ALL)
MONMSG (CPF9800) EXEC(GOTO ERRORS) /* All errors beginning CPF98… */
CHKOBJ Myfile *FILE MBR(Mymbr) AUT(*ALL)
MONMSG (CPF0000) EXEC(GOTO ERRORS) /* All errors beginning CPF…
CHKOBJ Myfile *FILE MBR(Mymbr) AUT(*ALL)
MONMSG (CPF0001 CPF9800 CPF9901 CPF9999) EXEC(GOTO ERRORS)
*/
Multiple MONMSG Commands
• Several MONMSG commands can be specified
for a single command when different actions
are required for different errors
CHKOBJ Myfile *FILE MBR(Mymbr) AUT(*ALL)
MONMSG CPF9801 EXEC(CRTPF Mylib/Myfile RCDLEN(80))
MONMSG CPF9802 EXEC(GOTO ERRORS)
Errors within a MONMSG
• By default, program-level MONMSG will
handle errors that occur for a command-level
MONMSG action
• Use DO group to alter default behavior
– Or to perform multiple MONMSG actions
CHKOBJ Myfile *FILE MBR(Mymbr) AUT(*ALL)
MONMSG CPF9801 EXEC(DO)
CRTPF Mylib/Myfile RCDLEN(80))
MONMSG CPF0000 EXEC(GOTO ERRORS)
ENDDO
MONMSG CPF9802 EXEC(GOTO ERRORS)
Ignoring Error Messages
• If you do not specify a MONMSG EXEC action,
error is ignored
• Program-level MONMSG should not usually
ignore messages
ADDLIBLE Mylib
MONMSG CPF2103
/* Already in library list */
Receiving Escape Messages
• RCVMSG puts escape message text into
program variable
• SNDPGMMSG can resend text to caller
PGM
DCL &msg *CHAR 80
MONMSG CPF0000 EXEC(GOTO ERROR)
ADDLIBLE Mylib
MONMSG CPF2103
/* Already in library list */
CHKOBJ Mylib/Myfile *FILE AUT(*ALL)
CALL Mypgm
RETURN
ERROR:
RCVMSG MSGTYPE(*LAST) MSG(&msg)
MONMSG CPF0000
/* Just in case */
SNDPGMMSG MSG(&msg)
MONMSG CPF0000
/* Just in case */
RETURN
ENDPGM