try: finally: question

Donn Cave donn at u.washington.edu
Tue Jul 6 21:12:41 CEST 2004


In article <O6mdnaNY5ceUeHfdRVn-ug at powergate.ca>,
 Peter Hansen <peter at engcorp.com> 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 = Device.open()
> 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 u.washington.edu



More information about the Python-list mailing list