try: finally: question

Donn Cave donn at
Tue Jul 6 21:12:41 CEST 2004

In article <O6mdnaNY5ceUeHfdRVn-ug at>,
 Peter Hansen <peter at> wrote:

> djw wrote:
> > I have a question regarding the "proper" use of try: finally:... Consider
> > some code like this:
> > [...]
> > When I run this code, and an Error is raised, I get traceback (which I don't
> > want) and the program terminates (which I don't want). 
> I'm sorry if I misinterpret what you want, but it sounds like you
> need nothing more than an enclosing try/except.
> d =
> try:
>     try:
>          d.someMethodThatCanRaiseError(...)
>          if SomeCondition:
>                  raise Error # Error is subclass of Exception
>          d.someMethodThatCanRaiseError(...)
>          ... lots of other methods on d that can raise Error
>     finally:
>          d.close()
> except:
>     # whatever you want here
> Is anything wrong with that?

Didn't seem to be what he wanted, though.  The best I could
make out was that he wanted a handler that receives control
on the exception, but leaves the exception in force enough
to cause an unwind to the next handler.  He has a specific
exception handler for each step in a procedure, but doesn't
want to explicitly decide, in the handler, where to go next.
Didn't really see what it had to do with "finally", except
that in his example it was the next handler.

   Donn Cave, donn at

More information about the Python-list mailing list