sds*_*sds 5 python exception python-3.x
例如,Python仅报告KeyError缺少的键,而不报告未找到键的字典。
我想在我的代码中“修复”此问题:
d = {1:"2"}
try:
d[5]
except Exception as e:
raise type(e)(*(e.args+(d,)))
----> 5 raise type(e)(*e.args+(d,))
KeyError: (5, {1: '2'})
Run Code Online (Sandbox Code Playgroud)
las,堆栈指向错误的行。
第二次尝试:
d = {1:"2"}
try:
d[5]
except Exception as e:
e.args += (d,)
raise e
----> 3 d[5]
KeyError: (5, {1: '2'})
Run Code Online (Sandbox Code Playgroud)
这里的堆栈是正确的。
这是正确的方法吗?有没有更好的办法?
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |