EXCEPTIONS AND ERROR TRAPPING





EXCEPTIONS AND ERROR TRAPPING

Error trapping and handling is a necessary part of all programming. There are many different specific reasons for trapping an error, but they basically come down to two basic reasons:

  1. Avoiding doing something that would cause the program/application to fail at a later stage. For example, checking that we've opened a file correctly before we start writing to it.

  2. Letting the user know when something has happened that either you didn't plan on or expect. For example, suddenly running out of disk space.

The model used by Perl is borrowed heavily from the C and shell script methods of checking the results of function calls. This presents some problems when dealing with certain errors, and also makes propagating errors within a script difficult. This is one of the reasons why we have the Carp module, it's there to facilitate error reporting that makes more sense to the programmer using the module, rather than the module programmer.

Python uses a different focus. Instead of having to check individual lines and function calls for problems, with Python we can "trap" an exception. The exception could be raised anywhere within a block of code, and we can trap and handle all of the different possible errors without having to explicitly check each line of the code.

In this chapter we'll start by looking at the Perl error system before having a quick overview of the Python exception system. Then we'll move on to looking at specific methods and systems that we can use within the Python exception system.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows