Programming:Python Exceptions - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Programming:Python Exceptions
 

Programming:Python Exceptions

Previous: Scoping Index Next: Input and output

Python 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.

Table of contents

Raising exceptions

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

>>> 1 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: integer division or modulo by zero

This traceback indicates that the ZeroDivisionError 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 try and except are used to catch exceptions. When an error occurs within the try block, Python looks for a matching except block to handle it. If there is one, execution jumps there.

If you execute this code:

try:
    print 1/0
except ZeroDivisionError:
    print "You can't divide by zero, you silly."

Then Python will print this:

You can't divide by zero, you silly.

If you don't specify an exception type on the except 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 except block is actually prepared to handle.

Exceptions can propagate up the call stack:

def f(x):
    return g(x) + 1

def g(x):
    if x < 0: raise ValueError, "I can't cope with a negative number here."
    else: return 5

try:
    print f(-6)
except ValueError:
    print "That value was invalid."

In this code, the print statement calls the function f. That function calls the function g, which will raise an exception of type ValueError. Neither f nor g has a try/except 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:

try:
   theFile = open("the_parrot")
except IOError, (ErrorNumber, ErrorMessage):
    if ErrorNumber == 2: # file not found
       print "Sorry, 'the_parrot' has apparently joined the choir invisible."
    else:
       print "Congratulations! You have managed to trip a #" + str(ErrorNumber) + " error:"
       print ErrorMessage

Which of course will print:

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

Trying over and over again

Recovering and continuing with finally

Builtin exception classes

All built-in Python exceptions (http://www.python.org/doc/current/lib/module-exceptions.html)

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 write clanky if statements to initialize the list; you can do:

   for newItem in newItems:
       try:
           self.items.append(newItem)
       except AttributeError:
           self.items = [newItem]

this is also much more efficient then an if statement because it assumes the code will succeed. In fact it will work 99% of the time :) An if statement would continue to get executed even after the array has been initialized.

Previous: Scoping Index Next: Input and output


Also helps finding: ProgrammingPythonExceptions, ProgrammingPython, PythonExceptions, programing, pithon, exeptions, programmin, pyton, execptions, progamming, pyhton, excpetions, prgramming, pythong, eceptions

   
 
  
Add to bookmarks
Related Articles
 
Programming:Python error handling
Programming:Python Scoping
Programming:Python Input and output
Programming:Python
Computer programming
Top Articles
 
Ada Programming
Ada Programming/Keywords/new
Ada Programming/Keywords/private
Ada Programming/Keywords/type
Cookbook:Butter
Cookbook:Chicken
Cookbook:Fruit
Cookbook:Pepper
Cookbook:Vegetable
GAT: A Glossary of Astronomical Terms
IP address
Japanese
Radiation Oncology
Radiation Oncology:RTOG Trials
SA NCS:Dramatic Arts
SA NCS:Economy
SA NCS:Electrical Technology
SA NCS:Engineering Graphics and Design
SA NCS:Qualification Assesment
South African Curriculum
Wikiversity
Search LiveJournal blogs for Programming:Python Exceptions
 

Free Myspace Layouts  •  Find jobs  •  Credit Consolidation  •  Millsberry •  Electronics

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.