Checking for the existence of an object

Before attempting to use an object in a program, check to determine if the object exists and if you have the authority to use it.

This is useful when a function uses more than one object at one time.

To check for the existence of an object, use the Check Object (CHKOBJ) command. You can use this command at any place in a procedure or program. The Check Object (CHKOBJ) command has the following format:


CHKOBJ  OBJ(library-name/object-name)  OBJTYPE(object-type)

Other optional parameters allow object authorization verification. If you are checking for authorization and intend to open a file, you should check for both operational and data authority.

When this command runs, the system sends messages to the program or procedure to report the result of the object check. You can monitor for these messages and handle them as you want.

In the following example, the Monitor Message (MONMSG) command checks only for the object-not-found escape message. For a list of all the messages which the Check Object (CHKOBJ) command may send see the online help information for the Check Object (CHKOBJ) command.

The Check Object (CHKOBJ) command does not allocate an object. For many application uses the check for existence is not an adequate function, the application should allocate the object. The Allocate Object (ALCOBJ) command provides both an existence check and allocation.

Use the Check Tape (CHKTAP) or Check Diskette (CHKDKT) command to ensure that a specific tape or diskette is placed on the drive and ready. These commands also provide an escape message that you can monitor for in your CL program.


           CHKOBJ  OBJ(OELIB/PGMA)  OBJTYPE(*PGM)
           MONMSG  MSGID(CPF9801)  EXEC(GOTO NOTFOUND)
           CALL OELIB/PGMA
           .
           .
           .
NOTFOUND:  CALL FIX001 /*PGMA Not Found Routine*/
           ENDPGM