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

Passing The Buck - 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
If one exception handler is nested within another, Python typically usesthe one closest to where the exception occurs. To illustrate this, considerthe 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 typeof exception. The outermost one is in the main program body, the next iswithin the popeye() function called from the main script, and the third iswithin the olive() function called by popeye().

When olive() runs, it generates a "NameError" exception, which isimmediately 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'taccount 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 callingpopeye() function, which would generate

$ nested_handlers.py Error in popeye
If popeye()'s exception handler didn't have the ability to handle theexception, the exception would move up even further, to the main body ofthe 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 killthe 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
 

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: