Errors from partial operations

Errors from partial operations#

Even well typed operations can fail. Division is the canonical partial operation, since it’s not defined when the divisor is 0. Here’s an example:

Here we get a ZeroDivisionError. Just like TypeError’s and NameError’s, errors like division by zero can be latent. Every programming language in common use has latent errors in partial operations.

We’ve seen other partial operations, like list.index. Here’s another example:

Here we get a ValueError when we try to find the index of an item not in the list.

Finally, list and string indexing is partial and can produce errors:

Here, we get an IndexError. We’d get it for string indexing too:

Notice that the messages accompanying the IndexError are slightly different: each message mentions the type that was being indexed.