cleaner way to write this?

Paul Rubin http
Wed Oct 25 16:12:50 EDT 2006


John Salerno <johnjsal at NOSPAMgmail.com> writes:
> Interesting idea to use try/finally to ensure that dlg.Destroy() runs
> even with a return earlier in the method. Would this be considered
> appropriate use though, or would it be misusing try/finally? I thought
> the point of a try block was for when you anticipated exceptions,

I think it's ok to treat the return statement as sort of an exception.
You could create an actual exception and raise it instead, but in such
a small function that clutters up the code unnecessarily.  At most I'd
add a comment near the return statement, mentioning that the finally:
clause will clean up the dialog.

> but given that try/except and try/finally used to be separate
> blocks,

That old separation was just an artifact of how the parser was
originally written, I believe.  It was always considered an annoyance
and it finally got fixed.



More information about the Python-list mailing list