Home arrow Python arrow Page 4 - Python 101 (part 8): An Exceptionally Clever Snake

Different Strokes - Python

In this concluding article of the Python 101 series, find out howto use Python's exception-handling routines to trap and resolve programerrors, learn to generate and use custom error messages, and get acquainteda bunch of useful (and free!) Python resources online.

TABLE OF CONTENTS:
  1. Python 101 (part 8): An Exceptionally Clever Snake
  2. Anatomy Of An Exception
  3. Trying Harder
  4. Different Strokes
  5. Passing The Buck
  6. Bad Boys
  7. Raising The Bar
  8. Strong Pythons (And The Exceptions That Love Them)
  9. The End Of The Affair
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 8
August 23, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
You can use a single "except" statement to handle more than one error byseparating the various exception names with commas and enclosing them inparentheses. Modifying the example above, we have

#!/usr/bin/python

try: alpha = input("Gimme a number: ") beta = input("Gimme another number: ") gamma = alpha / beta print alpha, "divided by", beta, "is", gamma except (ZeroDivisionError, OverflowError): print "You entered an illegal value!" else: print "No errors encountered!"

print "-- All done --"
If you take a close look at the stack trace on the previous page, you'llsee that when Python encounters an exception, it prints both an exceptionname and a descriptive string explaining the error.

ZeroDivisionError: integer division or modulo

OverflowError: integer literal too large

This descriptive text can also be caught and used by an exception handler,if you define a variable to store it in the "except" statement. Considerthe following code:

#!/usr/bin/python

try: alpha = input("Gimme a number: ") beta = input("Gimme another number: ") gamma = alpha / beta print alpha, "divided by", beta, "is", gamma except ZeroDivisionError, desc: print "Illegal value (", desc, ")" except OverflowError, desc: print "Illegal value (", desc, ")" else: print "No errors encountered!"

print "-- All done --"
In this case, the "desc" variable in the exception handler stores thedescriptive error message generated by Python; this variable may then beused within the handler.

Here's the output:

Gimme a number: 10 Gimme another number: 5 10 divided by 5 is 2 No errors encountered! -- All done --

Gimme a number: 10 Gimme another number: 0 Illegal value ( integer division or modulo ) -- All done --

Gimme a number: 897475834785348534785 Illegal value ( integer literal too large ) -- All done --

This works even if your exception handler is handling more than oneexception.

#!/usr/bin/python

try: alpha = input("Gimme a number: ") beta = input("Gimme another number: ") gamma = alpha / beta print alpha, "divided by", beta, "is", gamma except (ZeroDivisionError, OverflowError), desc: print "Illegal value (", desc, ")" else: print "No errors encountered!"

print "-- All done --"
Now, the "try" statement can only deal with exceptions that it knows about.What about the ones the developer can't predict?

Gimme a number: 76 Gimme another number: abc Traceback (innermost last): File "div.py", line 5, in ? beta = input("Gimme another number: ") File "gt;", line 0, in ? NameError: abc
It's possible to use a general "except" statement to handle *any* type ofexception generated by the interpreter - simply omit the exception namefrom the "except" statement. The following code snippet illustrates thistechnique:

#!/usr/bin/python

try: alpha = input("Gimme a number: ") beta = input("Gimme another number: ") gamma = alpha / beta print alpha, "divided by", beta, "is", gamma except: pass

print "-- All done --"
In this case, it doesn't matter what type of exception Python generates -the generic handler will catch it, ignore it and continue to process therest of the script.

Gimme a number: asd -- All done --

Gimme a number: 10 Gimme another number: 0 -- All done --

Gimme a number: 58439058349058934859 -- All done --

Gimme a number: 10 Gimme another number: 2 10 divided by 2 is 5 -- All done --

It should be noted, however, that this approach, although extremely simple,is *not* recommended for general use. It is poor programming practice totrap all errors, regardless of type, and ignore them; it is far better -and more professional - to anticipate the likely errors ahead of time, anduse the "try-except" construct to isolate and resolve them.

 
 
>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: