python异常链是什么
发布时间:2024-07-30 09:57:45

说明
1、通过except捕获异常A后,可以用raise句再次抛出异常B。
然后我们看到的异常信息是B的信息。但是我们不知道这个异常B来自哪里。此时,我们可以使用异常链。
2、使用raisefrom语句抛出异常链。
实例
>>>deffunc():
...raiseIOError
...
>>>try:
...func()
...exceptIOErrorasexc:
...raiseRuntimeError('Failedtoopendatabase')fromexc
...
Traceback(mostrecentcalllast):
File"<stdin>",line2,in<module>
File"<stdin>",line2,infunc
OSError
Theaboveexceptionwasthedirectcauseofthefollowingexception:
Traceback(mostrecentcalllast):
File"<stdin>",line4,in<module>
RuntimeError:Failedtoopendatabase以上是python异常链的介绍,希望对大家有所帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。
