pylint warning on 'except Exception:'

yanchenko picture yanchenko · Apr 16, 2009 · Viewed 22.4k times · Source

For a block like this:

try:
    #some stuff
except Exception:
    pass

pylint raises warning W0703 'Catch "Exception"'. Why?

Answer

Greg picture Greg · Apr 16, 2009

It's considered good practice to not normally catch the root Exception object, instead of catching more specific ones - for example IOException.

Consider if an out of memory exception occurred - simply using "pass" isn't going to leave your programme in a good state.

Pretty much the only time you should catch Exception is at the top level of your programme, where you can (try to) log it, display an error, and exit as gracefully as you can.