Python Programming/Exceptions

Python 2 handles all errors with exceptions.

An exception is a signal that an error or other unusual condition has occurred. There are a number of built-in exceptions, which indicate conditions like reading past the end of a file, or dividing by zero. You can also define your own exceptions.

Overview
Exceptions in Python at a glance:

Raising exceptions
Whenever your program attempts to do something erroneous or meaningless, Python raises exception to such conduct:

This traceback indicates that the  exception is being raised. This is a built-in exception -- see below for a list of all the other ones.

Catching exceptions
In order to handle errors, you can set up exception handling blocks in your code. The keywords  and   are used to catch exceptions. When an error occurs within the  block, Python looks for a matching   block to handle it. If there is one, execution jumps there.

If you execute this code: Then Python will print this:

You can't divide by zero!

If you don't specify an exception type on the  line, it will cheerfully catch all exceptions. This is generally a bad idea in production code, since it means your program will blissfully ignore unexpected errors as well as ones which the  block is actually prepared to handle.

Exceptions can propagate up the call stack:

In this code, the  statement calls the function. That function calls the function, which will raise an exception of type ValueError. Neither  nor   has a  /  block to handle ValueError. So the exception raised propagates out to the main code, where there is an exception-handling block waiting for it. This code prints:

That value was invalid.

Sometimes it is useful to find out exactly what went wrong, or to print the python error text yourself. For example:

Which will print:

Sorry, 'the_parrot' has apparently joined the choir invisible.

Custom Exceptions
Code similar to that seen above can be used to create custom exceptions and pass information along with them. This can be very useful when trying to debug complicated projects. Here is how that code would look; first creating the custom exception class:

And then using that exception:

Recovering and continuing with
Exceptions could lead to a situation where, after raising an exception, the code block where the exception occurred might not be revisited. In some cases this might leave external resources used by the program in an unknown state.

clause allows programmers to close such resources in case of an exception. Between 2.4 and 2.5 version of python there is change of syntax for  clause.


 * Python 2.4


 * Python 2.5

Built-in exception classes
All built-in Python exceptions

Exotic uses of exceptions
Exceptions are good for more than just error handling. If you have a complicated piece of code to choose which of several courses of action to take, it can be useful to use exceptions to jump out of the code as soon as the decision can be made. The Python-based mailing list software Mailman does this in deciding how a message should be handled. Using exceptions like this may seem like it's a sort of GOTO -- and indeed it is, but a limited one called an escape continuation. Continuations are a powerful functional-programming tool and it can be useful to learn them.

Just as a simple example of how exceptions make programming easier, say you want to add items to a list but you don't want to use "if" statements to initialize the list we could replace this: Using exceptions, we can emphasize the normal program flow—that usually we just extend the list—rather than emphasizing the unusual case: