except clause not catching IndexError

Derek Schuff dschuff at purdue.edu
Wed Feb 22 11:44:22 EST 2006


I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but
i couldnt find it.

I have some code like this:
        for line in f:
                toks = line.split()
                try:
                    if int(toks[2],16) == qaddrs[i]+0x1000 and toks[0] == "200": #producer
write
                        prod = int(toks[3], 16)
                    elif int(toks[2],16) == qaddrs[i]+0x1002 and toks[0] == "200":
#consumer write
                        cons = int(toks[3], 16)
                    else:
                        continue
                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

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.

any ideas?

thanks, 
-derek
 
  



More information about the Python-list mailing list