try: finally: question

djw donald.welch at
Wed Jul 7 18:51:35 CEST 2004

Peter Hansen wrote:

> Hmm... Also, I think it really *is* unusual (as in, "not usual"
> meaning not really common) to nest try blocks.  At least, I
> recall lone try/finally or try/except blocks being much much
> more common in most code than a mix.  Perhaps my memory is
> poor on this.  Perhaps someone will actually go and research
> it and prove it one way or the other.  If it really is
> very common, it might be worth revisiting the subject...
> If it's not very common, then this is once again just another
> discussion about syntactic sugar for an uncommon use case,
> and that kind of thing is so last year.  And the year before.
> And....
> -Peter

I am suprised that what I am trying to accomplish is unusual. Basically, I
want to acquire some resource/object (that requires cleanup). Then make
some calls against that object/resource, catching any excpetions along the
way (assuming that try/except is the right way to do this). If anything bad
happens, I want stop opererating on the object and insure that the object
is cleaned up (presumably a finally is the best way to do this). 

This seems like a really normal bit of code to me. I am suprised there isn't
a more elegant way to handle this (I thought that try/except/finally would
come to the rescue, but...) Is there a better way to handle this situation?


More information about the Python-list mailing list