Python
  Home arrow Python arrow Page 4 - Python 101 (part 8): An Exceptionally Clever Snake
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
PYTHON

Python 101 (part 8): An Exceptionally Clever Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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 by separating the various exception names with commas and enclosing them in parentheses. 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'll see that when Python encounters an exception, it prints both an exception name 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. Consider the 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 the descriptive error message generated by Python; this variable may then be used 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 one exception.

    #!/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 of exception generated by the interpreter - simply omit the exception name from the "except" statement. The following code snippet illustrates this technique:

    #!/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 the rest 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 to trap all errors, regardless of type, and ignore them; it is far better - and more professional - to anticipate the likely errors ahead of time, and use the "try-except" construct to isolate and resolve them.

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

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek