How to suppress displaying the parent exception (the cause) for subsequent exceptions

后端 未结 2 1587
遥遥无期
遥遥无期 2021-01-01 19:30

I\'m aware of raise ... from None and have read How can I more easily suppress previous exceptions when I raise my own exception in response?.

However,

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 20:12

    You can try suppressing the context yourself:

    try:
        value = cache_dict[key]
    except KeyError:
        try:
            value = some_api.get_the_value_via_web_service_call(key)
        except Exception as e:
            e.__context__ = None
            raise
    
        cache_dict[key] = value
    

提交回复
热议问题