In a slightly more interesting example we have a list of values, and we would like to divide a number by each one of the values. As you can see one of the values is 0 which will generate and exception. The loop will finish early.

examples/exceptions/divide_by_zero_list.py

from __future__ import print_function def div(a, b): print("dividing {:} by {:} is {:}".format(a, values[i], a/values[i])) a = 100 values = 2, 5, 0, 4 for i in range(0, len(values)): div(a, i) # dividing 100 by 2 is 50 # dividing 100 by 5 is 20 # Traceback (most recent call last): # File "examples/exceptions/divide_by_zero_list.py", line 10, in <module> # div(a, i) # File "examples/exceptions/divide_by_zero_list.py", line 4, in div # print("dividing {:} by {:} is {:}".format(a, values[i], a/values[i])) # ZeroDivisionError: integer division or modulo by zero

We can't repair the case where the code tries to divide by 0, but it would be nice if we could get the rest of the results as well.