Python
  Home arrow Python arrow Page 6 - 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 - Bad Boys
    ( Page 6 of 9 )

    Most of what you've just learned also applies to Python's other exception-handlng construct, the "try-finally" statement. The "try-finally" statement block differs from "try-except-else" in that it merely detects errors; it does not provide for a mechanism to resolve them. It is typically used to ensure that certain statements are always executed when an error (regardless of type) is encountered.

    The "try-finally" statement block looks like this:

    try: execute this block finally: if exceptions generated, execute this block
    If an exception is encountered when running the code within the "try" block, Python will stop execution at that point; jump to the "finally" block; execute the statements within it; and then pass the exception upwards, to the parent "try" block, if one exists, or to the default handler, which terminates the program and displays a stack trace.

    Here's an example:

    #!/usr/bin/python

    dessert = ('apple pie', 'chocolate fudge cake', 'icecream')

    try: # generate error by accessing index out of range print dessert[10] finally: print "Something bad happened"
    When this program runs, an IndexError exception will be generated and the "finally" block will execute, printing an error message. Control will then flow to the parent exception handler, which is the Python interpreter in this case; the interpreter will terminate the program and print a stack trace.

    $ dessert.py Something bad happened Traceback (innermost last): File "dessert.py", line 7, in ? print dessert[10] IndexError: tuple index out of range
    Since "try-finally" blocks simply detect errors, passing the resolution buck upwards to the parent "try" block, it's possible to nest them within "try-except-else" blocks. Take a look:

    #!/usr/bin/python

    try: dessert = ('apple pie', 'chocolate fudge cake', 'icecream')

    try: # generate error by accessing index out of range print dessert[10] finally: print "Something bad happened"

    except IndexError: print "You attempted to access a non-existent element. Bad boy!" except NameError: print "You attempted to access a non-existent object. What are you thinking?"
    Here's what'll happen when you run it:

    Something bad happened You attempted to access a non-existent element. Bad boy!


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