如何在循环中处理python异常?

我们可以通过如下重写代码来在代码中添加异常

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

我们得到以下输出

list index out of range
Process finished with exit code 0