The following example is very simple. I want to execute map() with a function which can raise Exception. It will be more clearly with an example :
number_list = range(-2,8)
def one_divide_by(n):
return 1/n
try:
for number, divide in zip(number_list, map(one_divide_by, number_list)):
print("%d : %f" % (number, divide))
except ZeroDivisionError:
# Execution is stopped. I want to continue mapping
pass
When I execute this code I get :
-2 : -0.500000
-1 : -1.000000
It's due to the 0 in my list. I don't want remove this 0 (because in real case I can't know first if I will get Exception). Do you know how to continue mapping after the exception ?