SunQuest
 
       Python
  Home arrow Python arrow Page 4 - Python 101 (part 8): An Exceptionally ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python 101 (part 8): An Exceptionally Clever Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2001-08-23

    Table of Contents:
  • Python 101 (part 8): An Exceptionally Clever Snake
  • Anatomy Of An Exception
  • Trying Harder
  • Different Strokes
  • Passing The Buck
  • Bad Boys
  • Raising The Bar
  • Strong Pythons (And The Exceptions That Love Them)
  • The End Of The Affair

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python 101 (part 8): An Exceptionally Clever Snake - Different Strokes


    (Page 4 of 9 )

    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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway