next up previous contents
Next: Using errno Up: Handling Errors and Signals Previous: Handling Errors and Signals

Checking for Errors

There is only one way to check for errors in any programming language. You need to test the return values of the functions that you call. Most functions return zero or false when something goes wrong. So when using a critical function like open() or sysread(), checking the return value helps to ensure that your program will work properly.

Perl has two special variables -- $? and $! - that help in finding out what happened after an error has occurred.

The variable, errno, is pre-defined variable that can sometimes be used to determine the last error that took place.

Caution You can't rely on these variables to check the status of pipes, back-quoted strings, or the system() function when executing scripts under the Windows operating system. My recommendation is to capture the output of the back-quoted string and check it directly for error messages. Of course, the command writes its errors to STDERR and then can't trap them, and you're out of luck.

Once you detect an error and you can't correct the problem without outside intervention, you need to communicate the problem to the user. This is usually done with the die() and warn() functions.


next up previous contents
Next: Using errno Up: Handling Errors and Signals Previous: Handling Errors and Signals
dave@cs.cf.ac.uk