except clause not catching IndexError
Erwin S. Andreasen
erwin+usenet at andreasen.org
Wed Feb 22 13:21:10 EST 2006
Derek Schuff <dschuff at purdue.edu> writes:
> I have some code like this:
[...]
> except IndexError: #happens if theres a partial line at the end of file
> print "indexerror"
> break
>
> 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
Did you by any chance do something like:
except ValueError, IndexError:
at some point earlier in this function? That, when catching ValueError
assigns the resulting exception to IndexError (and so the following
except IndexError: wouldn't work as IndexError is no longer what you
think it is). The correct syntax for catching multiple exceptions is:
except (ValueError, IndexError), targetVariable:
You could verify this by doing a print repr(IndexError) before your
line 202, to see that it really is the IndexError builtin.
--
===============================================================
<erwin+usenet at andreasen.org> London, E14
<URL:http://www.andreasen.org/> <*>
===============================================================
More information about the Python-list
mailing list