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

#!/usr/bin/env python from __future__ import print_function a = 100 values = 2, 5, 0, 4 for i in range(0, len(values)): print("dividing {:} by {:} is {:}".format(a, values[i], a/values[i])) # dividing 100 by 2 is 50 # dividing 100 by 5 is 20 # Traceback (most recent call last): # File "divide_by_zero_list.py", line 7, in <module> # 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.