Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python字典中找不到键时引发什么异常?_Python_Exception_Dictionary - Fatal编程技术网

在Python字典中找不到键时引发什么异常?

在Python字典中找不到键时引发什么异常?,python,exception,dictionary,Python,Exception,Dictionary,如果我有: map = { 'stack':'overflow' } try: map['experts-exchange'] except: <--- What is the Exception type that's thrown here? print( 'is not free' ) map={'stack':'overflow'} 尝试: map[“专家交流”] 除: 如果您在控制台上不使用try块执行此操作,它将告诉您 &

如果我有:

map = { 'stack':'overflow' }

try:
  map['experts-exchange']
except:                       <--- What is the Exception type that's thrown here?
  print( 'is not free' )
map={'stack':'overflow'}
尝试:
map[“专家交流”]
除:
如果您在控制台上不使用try块执行此操作,它将告诉您

>>> a = {}
>>> a['invalid']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'invalid'
>>> 
>a={}
>>>a[“无效的”]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
KeyError:“无效”
>>> 
Python 2.6.6(r266:8429220010年9月15日,16:22:56)
[GCC 4.4.5]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>map={a':'b'}
>>>打印地图['c']
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
键错误:“c”
>>> 

因此,一个粗略的猜测可能是…一个
KeyError

它被称为KeyError

>>d={1:2}

>>d[2]

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 2
>d={1:2}
>>d[2]
回溯(最近一次呼叫最后一次):
文件“”,第1行,是否在中?
关键错误:2

键错误

>>> x = {'try': 1, 'it': 2}
>>> x['wow']

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    x['wow']
KeyError: 'wow'
>x={'try':1,'it':2}
>>>x['wow']
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
x['wow']
关键错误:“哇”

如果您不知道要处理的特定异常,您可以简单地执行此类操作

map = {'stack': 'overflow'}

try:
    map['experts-exchange']
except Exception as inst:
    print(type(inst))       # the exception instance
    print(inst.args)        # arguments stored in .args
    print(inst)             # __str__ allows args to be printed directly,
                            # but may be overridden in exception subclasses
上述代码的输出是

<class 'KeyError'>
('experts-exchange',)
'experts-exchange'

(‘专家交流’,)
“专家交流”
当异常发生时,它可能有一个关联的值,也称为异常参数。参数的存在和类型取决于异常类型


except子句可以在异常名称后指定变量。变量绑定到异常实例,其参数存储在instance.args中。为方便起见,异常实例定义了参数,因此可以直接打印参数,而无需引用.args。也可以在引发异常之前先实例化异常,并根据需要向其添加任何属性。

您在哪里查找?页面上显示“d[key]——返回带有key的d项。如果key不在映射中,则会引发一个key错误。”我基本上在Bing中输入了“Python dictionary exception”,在前3个链接后就放弃了。我想我可以得到一个更快的答案=p但是谢谢你在这里添加了参考链接。你应该使用交互式控制台来查看这样的结果。我认为这个问题并不太糟糕,因为当有人搜索它时,它会显示在搜索引擎上,而不必搜索它,对吧?这有点讽刺,因为如果你搜索Python抛出的东西,如果你寻找一个不存在的键,这是谷歌搜索的最高结果。。。谢谢Shachris23谢谢你。在发布之前,我在控制台上尝试了它,但我不知道“KeyError”是实际的异常类型!noobness。Lol.选择您的答案是因为您的控制台建议使我意识到KeyError是异常名称。
map = {'stack': 'overflow'}

try:
    map['experts-exchange']
except Exception as inst:
    print(type(inst))       # the exception instance
    print(inst.args)        # arguments stored in .args
    print(inst)             # __str__ allows args to be printed directly,
                            # but may be overridden in exception subclasses
<class 'KeyError'>
('experts-exchange',)
'experts-exchange'