Why not just show the out-of-range index?

OKB (not okblacke) brenNOSPAMbarn at NObrenSPAMbarn.net
Mon Dec 4 13:52:34 EST 2006


Russ wrote:

> Every Python programmer gets this message occasionally:
> 
> IndexError: list index out of range
> 
> The message tells you where the error occurred, but it doesn't tell
> you what the range and the offending index are. Why does it force
> you to determine that information for yourself when it could save
> you a step and just tell you? This seems like a "no-brainer" to me.
> Am I missing something?

    	I think the same could be said of virtually all exceptions.  What I 
think would be ideal is that whenever an exception is raised, the 
traceback tells you:

    	1) What the exception is
    	2) The names of the variables involved in the offending expression 
(or their character position in the line)
    	3) The values of those variables

    	This would be especially useful in cases where you have some long 
expression and you get a "cannot concatenate str and list" or whatever.  
The irritating thing about this as it is is that you cannot tell which 
variables in the expression are causing the problem.

    	I realize that in some cases the offending expression may not be a 
single variable, but I am curious whether it would be possible for 
something like this:

"1" + "2" + "3" + "4" + 5 + "6"

    	To point to the actual addition that raises the exception (somewhat 
like it does for a syntax error), instead of just saying "there is an 
error somewhere in this line".

-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown



More information about the Python-list mailing list