Exceptions are powerful, but a single overzealous except clause can take it all away in a single line.
try: res = get_result() res = res log('got result: %r' % res) except: if not res: res = '' print('got exception')
This example demonstrates 3 symptoms of the antipattern:
exceptwith no exception type (line 5) will catch even healthy exceptions, including
KeyboardInterrupt. That will prevent the program from exiting in some cases.
reswas an empty list.
reswill stay completely unset, and the reference to
resin 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 except IndexError: res = '' log('got result: %r' % res)
We catch more specific exceptions, reraising where necessary. A few more lines, but infinitely more correct.