try: finally: question

Peter Hansen peter at engcorp.com
Tue Jul 6 13:44:40 EDT 2004


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?



More information about the Python-list mailing list