try: finally: question
djw
donald.welch at hp.com
Tue Jul 6 15:28:04 EDT 2004
Peter Hansen 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?
OK, yeah, I think you are correct. What wasn't apparent to me is that a
finally: doesn't stop the exception from continuing to propagate. I just
wish you could intermix except: and finally: within the same try: block.
Thanks,
Don
More information about the Python-list
mailing list