如果你需要捕获所有的异常,并且为所有异常做同样的事情,我会建议你:
try:
#stuff
except:
# do some stuff
如果你不想掩饰“特殊”的python异常,请使用Exception基类
try:
#stuff
except Exception:
# do some stuff
对于一些例外相关的管理,明确地明白:
try:
#stuff
except FirstExceptionBaseClassYouWantToCatch as exc:
# do some stuff
except SecondExceptionBaseClassYouWantToCatch as exc:
# do some other stuff based
except (ThirdExceptionBaseClassYouWantToCatch, FourthExceptionBaseClassYouWantToCatch) as exc:
# do some other stuff based
从python docs的exception hierarchy应该是一个有用的阅读。