getting the culprit from an exception

gyromagnetic gyromagnetic at excite.com
Sat Jul 28 20:48:00 EDT 2001


Hi,
I'm a relative newbie to Python. Is it possible to determine the
member of a list that caused an exception when using a 'mapped'
function?
As an example, suppose I have a list of items, some of which may not
be numbers and I would like to determine the first one that is not a
number.

I might use

try:
   new_list = map(float, mylist)
except ValueError:
   print "The item %s in mylist is not a number." % (my_item)


How do I get 'my_item'?

I realize that I can use "except ValueError, x:" and then perhaps pull
a piece out of x.args, but I'm sure there must be a better method.

Any help is appreciated.

-Brad

By the way, I don't have Python handy, so I'm not sure if ValueError
would be the proper exception in this case, but it should illustrate
the point.



More information about the Python-list mailing list