Overzealous except clause
suggest changeExceptions 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:
- The
exceptwith no exception type (line 5) will catch even healthy exceptions, includingKeyboardInterrupt. That will prevent the program from exiting in some cases. - The except block does not reraise the error, meaning that we won’t be able to tell if the exception came from within
get_resultor becausereswas an empty list. - Worst of all, if we were worried about result being empty, we’ve caused something much worse. If
get_resultfails,reswill stay completely unset, and the reference toresin the except block, will raiseNameError, 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents