Chúng ta có thể handle exception trong code bằng cách viết lại như sau:
a = []
foo = 'redbullz'
try:
for i in foo:
a.append(i)
print(a[8])
except Exception as e:
print(e)
Chúng ta nhận output sau:
list index out of range Process finished with exit code 0