我可以从 python 中的 finally 块中获取异常吗? [英] Can I get the exception from the finally block in python?
本文介绍了我可以从 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?
推荐答案
No,在finally
时间sys.exc_info
为all-None,是否有异常或不.使用:
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屋!
查看全文