try/finally exceptions dying

S

stephen.y4ng

Hello all,

In order to churn through a whole bunch of data sets (some good, some
bad..) and get all the errors at the end, (the data will be updated to
make it all good later on) I implemented a try/finally block with a
higher level handler to catch errors before they propagate to the
default handler and crash the program. Something like this:

def doStuff(args):
if(args says we should keep going):
try:
stuff
finally:
update args
doStuff(updated args)

def runWithErrorHandling():
try:
doStuff(args)
except (exception1, exception2), data:
<handle exception here...>
append exception data to a list of errors

runWithErrorHandling()

unfortunately, this churns through all the data and I only get the
last exception that occurred in the error list, not all of them, as I
expected. What is going on? I thought finally always propagated the
exception all the way up the stack until it was handled.

Any thoughts are appreciated.

Stephen
 
M

Matt McCredie

I thought finally always propagated the
exception all the way up the stack until it was handled.

Finally will propagate the exception up, unless another exception
occurs within the finally block. Since there is not (yet:
http://www.python.org/dev/peps/pep-3134/) exception chaining in
Python, only the last exception to be raised is held onto.

The finally block combined with the recursion makes it impossible to
catch every exception that way.

Something like this might work:

Code:
def dostuff(args):
   stuff
   return status

def run_with_error_handling():
    exceptions = []
    done = False
    while 1:
        try:
          status = dostuff(args)
        except (exception1, exception2), data:
            exceptions.append(data)
            modify status
         finally:
            update args based on status
            break if done

Note that in the above example, status could be just a tuple of args.
Then you just need to figure out how to modify the args in the
exception case.

The above leaves a lot of holes though. If you want more help you will
have to send a more complete example.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top