From:How to properly ignore Exceptions?shell
try: doSomething() except: pass
orspa
try: doSomething() except Exception: pass
區別:code
代碼1將捕獲KeyboardInterrupt、SystemExit等由exceptions.BaseException直接派生出的相似異常。blog
代碼2將捕獲由exceptions.Exception直接派生出的異常。get
若是須要捕獲全部異常,catch Exception而不是僅僅使用except:語句。it
except:語句會捕獲包括SystemExit的異常。io
例如:class
>>> try: ... sys.exit(1) ... except: ... pass ... >>>
>>> try: ... sys.exit(1) ... except Exception: ... pass ... shell:~$