我可以从python中的finally块获取异常吗? [英] Can I get the exception from the finally block in python?

查看:78
本文介绍了我可以从python中的finally块获取异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本中有一个 try / finally 子句。可以从 finally 子句中获取确切的错误消息。

I have a try/finally clause in my script. Is it possible to get the exact error message from within the finally clause?

推荐答案

不,在终于时间 sys.exc_info 是全无,是否有异常
或不。使用:

No, at finally time sys.exc_info is all-None, whether there has been an exception or not. Use:

try:
  whatever
except:
  here sys.exc_info is valid
  to re-raise the exception, use a bare `raise`
else:
  here you know there was no exception
finally:
  and here you can do exception-independent finalization

这篇关于我可以从python中的finally块获取异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆