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:


Python Anti-Patterns:
* Overzealous except clause

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
121 Python Anti-Patterns
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve