在python中使用多个异常

Ham*_*FzM 3 python exception-handling exception python-3.x

有没有办法在python中使用多个异常?像下面的代码:

try:
   #mycode
except AttributeError TypeError ValueError:
   #my exception
Run Code Online (Sandbox Code Playgroud)

我的意思是如何相互使用 AttributeError TypeError ValueError

Mar*_*ers 8

使用元组:

try:
   # mycode
except (AttributeError, TypeError, ValueError):
   # catches any of the three exception types above
Run Code Online (Sandbox Code Playgroud)

引用参考try声明文档:

try套件中发生异常时,将启动对异常处理程序的搜索.此搜索依次检查except子句,直到找到与该异常匹配的子句.
[...]
对于带有表达式的except子句,将对该表达式求值,如果结果对象与异常"兼容",则子句匹配异常.如果对象是异常对象的类或基类,或者包含与异常兼容的项的元组,则该对象与异常兼容.

强调我的.