Python Language Do not catch everything!


While it's often tempting to catch every Exception:

except Exception:
    # log / try to reconnect / exit gratiously
    print "The END"
    # it runs no matter what execute.

Or even everything (that includes BaseException and all its children including Exception):

    pass  # do whatever needed

In most cases it's bad practice. It might catch more than intended, such as SystemExit, KeyboardInterrupt and MemoryError - each of which should generally be handled differently than usual system or logic errors. It also means there's no clear understanding for what the internal code may do wrong and how to recover properly from that condition. If you're catching every error, you wont know what error occurred or how to fix it.

This is more commonly referred to as 'bug masking' and should be avoided. Let your program crash instead of silently failing or even worse, failing at deeper level of execution. (Imagine it's a transactional system)

Usually these constructs are used at the very outer level of the program, and will log the details of the error so that the bug can be fixed, or the error can be handled more specifically.