a_list = ['First error', 'Second error']
try:
print(a_list[3])
except Exception as e:
try:
# the following line is a developer mistake - they wanted to print progress as 1/10 but wrote 1/0
print(1 / 0)
except ZeroDivisionError as f:
print('Inner exception (f):', f)
print('Outer exception (e):', e)
print('Outer exception referenced:', f.__context__)#为什么这里输出的是e的信息
print('Is it the same object:', f.__context__ is e)
"""
output:
Inner exception (f): division by zero
Outer exception (e): list index out of range
Outer exception referenced: list index out of range
Is it the same object: True
"""
python try excepion
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- CSDN专家-showbo 2022-07-13 12:57关注
下面是官网的解释
When raising a new exception while another exception is already being handled, the new exception's context attribute is automatically set to the handled exception. An exception may be handled when an except or finally clause, or a with statement, is used.
大概意思就是
当另一个异常已经被处理时引发一个新异常,新异常的 __context__属性会自动设置为已处理的异常
。except当使用or finally子句或语句时,可能会处理异常with。内置异常 — Python 3.10.5 文档 https://docs.python.org/zh-cn/3.10/library/exceptions.html本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥500 火焰左右视图、视差(基于双目相机)
- ¥100 set_link_state
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本