The Danger of Exceptions

An unhandled exception can stop a program dead in its tracks. This may be okay if you're still developing it, but it's bad if the program has been delivered. Consider the following function (from divby()-1.py), which stops as soon as it runs into a 0 value as a denominator in the list.

def figurePercentage(figures):
      for tuple in figures:
            numerator = tuple[0]
            denominator = tuple[1]
            percent = numerator/denominator
            print ("The percentage is " + `percent`)

Here the function figurePercentage() expects to receive a sequence of sequences that each contain two numeric fields. So we create a list sequence that contains several tuples, each containing two numeric values, and we invoke figurePercentage() with the sequence as follows: ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.