Exceptions are powerful, but a single overzealous except clause can take it all away in a single line.
try:
res = get_result() res = res[0] log(‘got result: %r’ % res)
except:
if not res: res = ‘’ print(‘got exception’)
This example demonstrates 3 symptoms of the antipattern:
except
with no exception type (line 5) will catch even healthy exceptions, including [KeyboardInterrupt](<https://docs.python.org/2/library/exceptions.html#exceptions.KeyboardInterrupt>)
. That will prevent the program from exiting in some cases.get_result
or because res
was an empty list.get_result
fails, res
will stay completely unset, and the reference to res
in the except block, will raise [NameError](<https://docs.python.org/2/library/exceptions.html#exceptions.NameError>)
, completely masking the original error.Always think about the type of exception you’re trying to handle. Give the exceptions page a read and get a feel for what basic exceptions exist.
Here is a fixed version of the example above:
import traceback
try:
res = get_result()
except Exception:
log_exception(traceback.format_exc()) raise
try:
res = res[0]
except IndexError:
res = ''
log('got result: %r' % res)
We catch more specific exceptions, reraising where necessary. A few more lines, but infinitely more correct.