[英]What exception type should I raise if my dictionary is missing required keys?
I am testing that data
(dictionary), used in Django's render(request, url, data)
, has some required keys. 我正在测试在Django的
render(request, url, data)
使用的data
(字典)具有一些必需的键。 I will raise an exception if the required keys are missing. 如果缺少必需的键,我将引发一个异常。 What exception type is best suited for this?
哪种异常类型最适合此?
Is there a better way to think about this? 有没有更好的方式考虑这一点?
For consistency with regular dicts, I would think a KeyError
would be appropriate 为了与常规命令保持一致,我认为
KeyError
是合适的
>>> d = {'foo': 'bar'}
>>> d[5]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
d[5]
KeyError: 5
Raised when a mapping (dictionary) key is not found in the set of existing keys.
在现有键集中找不到映射(字典)键时引发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.