KraftDiner wrote:
> I have a dictionary and sometime the lookup fails...
> it seems to raise an exception when this happens.
> What should I do to fix/catch this problem?
> 
>     desc = self.numericDict[k][2]
> KeyError: 589824   <---- This is the error that is being produced,
> because there is no key
> 589824.
> 

As stated you can wrap the access in the try - except - else statement, 
as in

try:
  foo['bar']
except :
  # Handle the error.
  pass
else :
  # We have it, so use it...
  print foo['bar']

Or you can use the has_key() and test it first. For example

  if foo.has_key('bar'):
        print 'we have it'
  else :
        print 'we don't have bar.'

That'll do it for me.

Chaz.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to