Chapter 9. Exception Handling

Exceptions are a solution to a difficult problem: How can programs deal with unexpected errors? For instance, what happens if a file disappears in the middle of being read because some other program on your computer has deleted it? Or what if the Web site your program is downloading pages from suddenly crashes?

In these and many other situations, what Python does is raise an exception. An exception is a special kind of error object that you can catch and then examine in order to determine how to handle the error.

Exceptions can change the flow of control of your program. Depending on when it occurs, an exception can cause the flow of control to jump out of the middle of a function or loop into another block of code ...

Get Python: Visual QuickStart Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.