try/finally exceptions dying

Matt McCredie mccredie at gmail.com
Tue Aug 14 16:42:53 EDT 2007


> 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
[/code]

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



More information about the Python-list mailing list