Python
  Home arrow Python arrow Page 5 - 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 - Passing The Buck
    ( Page 5 of 9 )

    If one exception handler is nested within another, Python typically uses the one closest to where the exception occurs. To illustrate this, consider the following code snippet:

    #!/usr/bin/python

    # nested_handlers.py

    def popeye(): try: olive() except NameError: print "Error in popeye"

    def olive(): try: print someUnnamedVar except NameError: print "Error in olive"

    try: popeye() except NameError: print "Error in main"
    In this case, there are three exception handlers defined for the same type of exception. The outermost one is in the main program body, the next is within the popeye() function called from the main script, and the third is within the olive() function called by popeye().

    When olive() runs, it generates a "NameError" exception, which is immediately handled by its own "try" block.

    $ nested_handlers.py Error in olive
    However, if olive() didn't have a "try" block, or its "try" block didn't account for "NameError" exceptions,

    #!/usr/bin/python

    def popeye(): try: olive() except NameError: print "Error in popeye"

    def olive(): print someUnnamedVar

    try: popeye() except NameError: print "Error in main"
    the exception would be passed up to the previous level, the calling popeye() function, which would generate

    $ nested_handlers.py Error in popeye
    If popeye()'s exception handler didn't have the ability to handle the exception, the exception would move up even further, to the main body of the script,

    #!/usr/bin/python

    def popeye(): try: olive() # this wouldn't handle NameError except IndexError: print "Error in popeye"

    def olive(): print someUnnamedVar

    try: popeye() except NameError: print "Error in main"
    which would result in

    $ nested_handlers.py Error in main
    And if there weren't any handlers capable of resolving the error,

    #!/usr/bin/python

    def popeye(): olive()

    def olive(): print someUnnamedVar

    popeye()
    the exception would be processed by the default handlers, which would kill the script and print a stack trace.

    Traceback (innermost last): File "./test.py", line 9, in ? popeye() File "./test.py", line 4, in popeye olive() File "./test.py", line 7, in olive print someUnnamedVar NameError: someUnnamedVar


     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek