繁体   English   中英

如何在python中捕获所有旧式类异常?

[英]How to catch all old-style class exceptions in python?

在代码中有不同的旧式类,如下所示:

class customException: pass

以这种方式提出异常:

raise customException()

是否有一种类型可以捕获所有那些旧式的类异常? 像这样:

try:
    ...
except EXCEPTION_TYPE as e:
    #do something with e

或者至少有一种方法可以捕获所有内容(旧式和新式)并在变量中获取异常对象?

try:
    ...
except:
    #this catches everything but there is no exception variable 

我能想到的唯一解决方案是使用sys.exc_info

import sys
try:
    raise customException()
except:
    e = sys.exc_info()[1]
    # handle exception "e" here...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM