Alex Rivera | Logout

What exception is thrown when key is not found in Python dictionary?

Asked 2010-11-17T18:27:24.880
47

If I have:

map = { 'stack':'overflow' }

try:
  map['experts-exchange']
except:                       <--- What is the Exception type that's thrown here?
  print( 'is not free' )

Couldn't find it on the web. =(

Edit
Report

1 Answer

3
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> map = { 'a' : 'b' }
>>> print map['c']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'c'
>>> 

So a wild guess might be...a KeyError ?

answered 2010-11-17T18:29:11.333

Your Answer