next up previous contents
Next: Checking for Errors Up: Practical Perl Programming Previous: Example: Using the %ENV

Handling Errors and Signals

So far most of the examples we have developed have been ignoring the fact that errors can and probably will occur. An error can occur because the directory you are trying to use does not exist, the disk is full, or any of a thousand other reasons. Quite often, you won't be able to do anything to recover from an error, and your program should exit. However, exiting after displaying a user-friendly error message is much preferable than waiting until the operating system or Perl's own error handling takes over.

After looking at errors generated by function calls, we'll look at a way to prevent certain normally fatal activities-like dividing by zero-from stopping the execution of your script; this is by using the eval() function.

Then, you'll see what a signal is and how to use the %SIG associative array to create a signal handling function.