【发布时间】:2022-02-05 03:55:41
【问题描述】:
如果我捕获到KeyError,我如何判断查找失败的原因?
def poijson2xml(location_node, POI_JSON):
try:
man_json = POI_JSON["FastestMan"]
woman_json = POI_JSON["FastestWoman"]
except KeyError:
# How can I tell what key ("FastestMan" or "FastestWoman") caused the error?
LogErrorMessage ("POIJSON2XML", "Can't find mandatory key in JSON")
【问题讨论】:
-
由于无论如何您都必须在失败的键上进行分支,因此将每个查找放在单独的
try语句中可能更清楚。
标签: python python-3.x