Get*_*ree 6 python exception-handling python-2.x
在代码中有不同的旧式类,如下所示:
class customException: pass
Run Code Online (Sandbox Code Playgroud)
以这种方式提出异常:
raise customException()
Run Code Online (Sandbox Code Playgroud)
是否有一种类型可以捕获所有那些旧式的类异常?像这样:
try:
...
except EXCEPTION_TYPE as e:
#do something with e
Run Code Online (Sandbox Code Playgroud)
或者至少有一种方法可以捕获所有内容(旧式和新式)并在变量中获取异常对象?
try:
...
except:
#this catches everything but there is no exception variable
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一解决方案是使用sys.exc_info
import sys
try:
raise customException()
except:
e = sys.exc_info()[1]
# handle exception "e" here...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |