Chapter 4. Expecting the Unexpected

Programs are very fragile. It would be nice if code always returned a valid result, but sometimes a valid result can't be calculated. It's not possible to divide by zero, or to access the eighth item in a five-item list, for example.

In the old days, the only way around this was to rigorously check the inputs for every function to make sure they made sense. Typically functions had special return values to indicate an error condition; for example, they could return a negative number to indicate that a positive value couldn't be calculated. Different numbers might mean different errors occurred. Any code that called this function would have to explicitly check for an error condition and act accordingly. A lot of ...

Get Python 3 Object Oriented Programming 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.