How to properly ignore exceptions

Joan Venge picture Joan Venge · Apr 8, 2009 · Viewed 1M times · Source

When you just want to do a try-except without handling the exception, how do you do it in Python?

Is the following the right way to do it?

try:
    shutil.rmtree(path)
except:
    pass

Answer

vartec picture vartec · Apr 8, 2009
try:
    doSomething()
except: 
    pass

or

try:
    doSomething()
except Exception: 
    pass

The difference is that the first one will also catch KeyboardInterrupt, SystemExit and stuff like that, which are derived directly from exceptions.BaseException, not exceptions.Exception.

See documentation for details: