以下两段代码都做同样的事情。他们捕获每个异常并执行 except:
块中的代码
片段 1 -
try:
#some code that may throw an exception
except:
#exception handling code
片段 2 -
try:
#some code that may throw an exception
except Exception as e:
#exception handling code
这两种结构到底有什么区别?
原文由 narendranathjoshi 发布,翻译遵循 CC BY-SA 4.0 许可协议
在第二个中,您可以访问异常对象的属性:
但是它不会捕获
BaseException
GeneratorExit
KeyboardInterrupt
验证异常SystemExit
一个简单的除外:
有关详细信息,请参阅文档的 内置异常 部分和教程的 错误和异常 部分。