[Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program?
Peter Otten
__peter__ at web.de
Fri Mar 20 16:37:51 CET 2015
Albert-Jan Roskam wrote:
>>I would expect that
>>
>>try:
>>main program here
>>finally:
>>close_relay()
>
> Is this (also) called a diaper pattern?
Cool name, but most parents want the baby's faeces to go into the diapers
whereas try ... bare except is notorious for swallowing useful information
that should be propagated to the user or developer.
> Or is that name reserved for the
> antipattern with try-bare except, where the 'except' catches all the sh*t
> (pardon my language)?
For try ... finally it's its raison d'ĂȘtre to execute some code no matter
what. E. g. before the advent of with ...
with open(...) as f:
... # use file
# file is closed now even if something went wrong while using it and no
# matter how garbage collection works for the Python implementation running
# the code.
# The exception (if any) is not swallowed
it was good style to write
f = open(...)
try:
... # use file
finally:
f.close()
# file is closed...
More information about the Tutor
mailing list