Next: Using the || Logical
Up: Handling Errors and Signals
Previous: Checking for Errors
When an error occurs, it is common practice for UNIX-based functions
and programs to set a variable called errno
to reflect which error has occurred. If errno=2,
then your script tried to access a directory or file that did
not exist. Table 13.1 lists 10 possible values the errno
variable can take, but there are hundreds more. If you are interested
in seeing all the possible error values, run the program,err1.pl:
for ($! = 1; $! <= 10000; $!++) {
$errText = $!;
chomp($errText);
printf("%04d: %s\n", $!, $errText) if $! ne "Unknown Error";
}
The program operates as follows:
- Loop from 1 to 10,000 using $!
as the loop variable.
- Evaluate the $!
variable in a string context so that $errText
is assigned the error message associated with the value of
$!
- Use chomp()
to eliminate possible newlines at the end of an error message.
- Some of the messages have newlines, and some don't.
- Print the error message if the message is not Unknown
Error.
- Any error value not used by the system
defaults to Unknown Error.
- Using the if statement modifier ensures that only valid error
messages are displayed.
There are Ten Possible Values for
errno:
- 1.
- Operation not permitted
- 2.
- No
such file or directory
- 3.
- No such process
- 4.
- Interrupted function call
- 5.
- Input/output error
- 6.
- No such device or address
- 7.
- Arg list too long
- 8.
- Exec format error
- 9.
- Bad file descriptor
- 10.
- No child processes
Next: Using the || Logical
Up: Handling Errors and Signals
Previous: Checking for Errors
dave@cs.cf.ac.uk