Overzealous except clause

suggest change

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:

  1. The except with no exception type (line 5) will catch even healthy exceptions, including KeyboardInterrupt. That will prevent the program from exiting in some cases.
  2. The except block does not reraise the error, meaning that we won’t be able to tell if the exception came from within get_result or because res was an empty list.
  3. Worst of all, if we were worried about result being empty, we’ve caused something much worse. If get_result fails, res will stay completely unset, and the reference to res in the except block, will raise 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents