Error Handlers
Another approach that is more complex for the programmer but much
more convenient for the user is to check for specific values of errno and
execute error routines related to the value. In most cases, only a limited
number of situations can cause a particular a subroutine to fail, so there is a
correspondingly small number of errno values that can be encountered upon
failure. Possible error values are usually listed in the HP- UX Reference on the
manual page entry for the failed subroutine.
For example, checking open(2) in the HP- UX Reference reveals that errno is
set to
ENOENT
(defined in the errno.
h
header file) if you attempt to open a file
that does not exist and you have not given the system call permission to create
a new file. Armed with this information, you can incorporate the following
code segment in your program:
#include <errno.h>
#include <fcntl.h>
maine)
{
}
int eid;
if «eid
=
open ("/dev/raw_hpib" . O_RDWR»
==
-1)
{
}
if (errno
==
ENOENT)
printf("Error: cannot open; file does not exist\n");
else
printf("Error: file exists but cannot open\n");
exit
(1) ;
Note that the print statements in the example above could be replaced with
calls to more sophisticated error-handling routines such as perror (see the
perror(3C) entry in the HP- UX Reference).
2-12
General-Purpose Routines