简体   繁体   English

我什么时候应该在python中引发LookupError?

[英]When should I raise LookupError in python?

Python's built-in exception documentation defines LookupError as: Python的内置异常文档将LookupError定义为:

The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: IndexError, KeyError. 当映射或序列上使用的键或索引无效时引发的异常的基类:IndexError,KeyError。 This can be raised directly by codecs.lookup(). 这可以通过codecs.lookup()直接引发。

Should this base class be used only when catching try sections that access dictionaries using both indices and keys when one wants to shorthand catching both, or is there another case where you would use it? 是否应该仅在捕获尝试使用索引和键来访问字典的部分时才使用此基类,当想要快速捕获两者时,或者是否有另外使用它的情况?

First of all dictionaries only use keys (see: How to index into a dictionary? ). 首先,字典只使用键(参见: 如何索引字典? )。

If you are a lazy person you cold catch both KeyError and IndexError with the LookupError (lest say you have a dictionary filled with lists). 如果你是一个懒惰的人,你会使用LookupError同时捕获KeyError和IndexError(以免你说有一个字典填充了列表)。 Never the less i would prefer to catch them separately with two different exceptions. 从来没有我更愿意分别用两种不同的例外来捕捉它们。 Eg: 例如:

try:
    # do some stuff here
except KeyError:
    # key error handling
except IndexError:
    # index error handling

This way you can respond to these exceptions in different ways, as they were caused by different events. 这样,您可以以不同的方式响应这些异常,因为它们是由不同的事件引起的。 Furthermore there might be other exceptions that are a variation of a LookupError (see below) and you do not want to catch these exceptions as well (same reason one does not simply use except: ). 此外,可能还有其他异常是LookupError的变体(见下文),并且您也不希望捕获这些异常(同样的原因except:不会简单地使用:)。

Another way to use the LookupError could be if you are in need of your own exception, as your error that this exception represents is nether described by a KeyError, nor an IndexError, but is a type of LookupError. 使用LookupError的另一种方法是,如果您需要自己的异常,因为此异常所代表的错误是由KeyError描述的错误,也不是IndexError,而是一种LookupError。 In this case your custom exception could inherit from LookupError. 在这种情况下,您的自定义异常可以从LookupError继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当请求非法状态时我应该提出什么Python异常? - What Python Exception should I raise when illegal state is asked for? 当没有更多元素可以产生时,Python生成器是否应该引发异常? - Should a Python generator raise an exception when there are no more elements to yield? 对于 Python 中的错误/非法参数组合,我应该引发哪个异常? - Which exception should I raise on bad/illegal argument combinations in Python? 发生异常时,我应该提出还是冒犯? - When exception occurs should I raise or bubble it up? Python:LookupError:未知编码:十六进制 - Python: LookupError: unknown encoding: hex python什么时候引发FloatingPointError? - When does python raise a FloatingPointError? 我应该为只能调用一次的函数引发python中的哪种标准异常 - What kind of the standard exception in python should I raise for a function which can only be invoked once 当列表大小不是某个值的倍数时,应该引发什么错误? - What error should I raise when a list size is not a multiple of some value? 我认为这应该引发错误,但事实并非如此 - I think this should raise an error, but it doesn't LookupError:未安装标签为“ admin”的应用。 当我尝试使用Django运行程序时 - LookupError: No installed app with label 'admin'. when I tried running a program using Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM