Stopping exception unwinding at exit

David Brady daves_spam_dodging_account at yahoo.com
Tue Nov 13 15:42:00 EST 2001


Hello,

I'm trying to write a robust script, one that recovers
gracefully from just about any condition.  Because I'm
new to Python, I've wrapped my entire script in a try:
block that goes into a friendly exception dumper that
lets me know that some unexpected exception was
raised, and provides a dump of it as a StringIO,
suitable for logging to a file.

I have, inside the first try block, an exception
handler that catches a specific error, does the dump
thing, and then calls sys.exit(-1).

The problem I'm seeing is that, because the inner
block raised an exception, after the call to
sys.exit(-1), the outer block catches it and also
dumps the exception.  Is there a way to turn that off?
 Some way to tell Python that the exception has been
handled?

Here is a dramatically simplified version of the
script.  Sorry, I don't know how to do this from the
interactive prompt since it uses sys.exit(), but
here's the script and the output:

#------------------------
# SCRIPT
import sys

def Puke(msg):
    print "Mr. Creosote: BARF!\n%s" % msg
    sys.exit(-1)

try:
    try:
        x = 1
        y = 0
        z = x/y
    except:
        Puke("Can't divide by zero")
except:
    Puke("An unknown exception was raised in the
script.")

#------------------------
# OUTPUT
Mr. Creosote: BARF!
Can't divide by zero
Mr. Creosote: BARF!
An unknown exception was raised in the script.

Thank you,

-dB


=====
David Brady
daves_spam_dodging_account at yahoo.com
I'm feeling very surreal today... or *AM* I?

__________________________________________________
Do You Yahoo!?
Find the one for you at Yahoo! Personals
http://personals.yahoo.com




More information about the Python-list mailing list