How to handle a python exception within a loop?

PythonServer Side ProgrammingProgramming

We can the exception in the code by rewriting it as follows

a=[]
foo = 'redbullz'
try:
for i in foo:
a.append(i)
print a[8]
except Exception as e:
print e

We get following output

list index out of range
Process finished with exit code 0
raja
Published on 07-Dec-2017 06:55:03
Advertisements