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

Bad Boys - 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
Most of what you've just learned also applies to Python's otherexception-handlng construct, the "try-finally" statement. The "try-finally"statement block differs from "try-except-else" in that it merely detectserrors; it does not provide for a mechanism to resolve them. It istypically used to ensure that certain statements are always executed whenan 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 exceptionupwards, to the parent "try" block, if one exists, or to the defaulthandler, 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 thenflow to the parent exception handler, which is the Python interpreter inthis case; the interpreter will terminate the program and print a stacktrace.

$ 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 resolutionbuck 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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: