Chapter 9. Exceptions
Python's general approach to unexpected situations is to raise an exception. The idea is that an operation should either work normally and completely, or raise an exception. In some languages, complex numeric status codes are used to indicate success. In Python, success is assumed; if there's a problem, an exception is raised to indicate that the operation did not succeed.
Exceptions can be raised by all aspects of Python programs. All of the built-in classes involve exceptions for various kinds of unexpected conditions. Many library packages define their own unique exceptions which extend the built-in hierarchy of exceptions.
We'll look at the essential concept behind exceptions first. Python has a number of statements that we'll use. The raise
statement creates an exception object. The try
statement allows us to deal with exceptions.
The except
clause in a try
statement is used to match the class of exception being raised. With some kinds...