MagicforestA 2022-07-13 11:57 采纳率: 100%
浏览 30
已结题

python try excepion

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
"""
  • 写回答

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。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月21日
  • 已采纳回答 7月13日
  • 创建了问题 7月13日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大