except clause not catching IndexError

Scott David Daniels scott.daniels at acm.org
Wed Feb 22 12:38:42 EST 2006


Derek Schuff wrote:
> I have some code like this:
         <code nobody else can run>

> However, when I run it, it seems that I'm not catching the IndexError:
> Traceback (most recent call last):
>   File "/home/dschuff/bin/speeds.py", line 202, in ?
>     if int(toks[2],16) == qaddrs[i]+0x1000 and toks[0] == "200": #producer
> write
> IndexError: list index out of range

This was good, the actual error; I suspect you overwrote IndexError.
The general principal is to boil down your code to the smallest code
that exhibits the problem.  This is not just to aid us, since usually
at some point you delete a block of lines and the problem goes
magically away.

> If i change the except IndexError to except Exception, it will catch it (but
> i believe it's still an IndexError). This is python 2.3 on Debian sarge.
More points for identifying Python version and OS.

> any ideas?
As above, but to test my theory:

...
     except Exception, e:
         print 'caught %r: %r (IndexError is %r)' % (
                      e, e.__class__, IndexError)

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list