Page 124 -
P. 124
exceptional catch
Handle exceptions
Have you noticed that when something goes wrong with your code, the
Python interpreter displays a traceback followed by an error message?
The traceback is Python’s way of telling you that something unexpected has
occurred during runtime. In the Python world, runtime errors are called
exceptions.
Whoooah! I don’t
know what to do with this
error, so I’m gonna raise
an exception...this really is
someone else’s problem.
>>> if not each_
Traceback (most r
Oooh, yuck! It looks File “<pyshell
like there’s a bug.
(role, line_
ValueError: too m
Of course, if you decide to ignore an exception when it occurs, your program
crashes and burns.
But here’s the skinny: Python let’s you catch exceptions as they occur, which
gives you with a chance to possibly recover from the error and, critically, not
crash.
By controlling the runtime behavior of your program, you can ensure (as
much as possible) that your Python programs are robust in the face of most
runtime errors.
Try the code first. Then deal with errors as they happen.
88 Chapter 3

