about try and exception

Alex Martelli aleax at mail.comcast.net
Thu Nov 17 20:25:16 EST 2005


Shi Mu <samrobertsmith at gmail.com> wrote:
   ...
> If we do not know when the error will happen during the calculation
> but still want to continue till end, what should I do?
> for example:
> 
> def test(a,b,c):
>     return a/(b-c)
> q=[1,1,2,2,4,6,9,0]
> for i in range(len(q)):
>     print test(q[i],q[i+1],q[i+2])

put the try/except around the print statement (you'll also need to
decide whether you also want to catch the IndexError you'll get towards
the end of the sequence, of course;-).


Alex



More information about the Python-list mailing list