Learning Python - Mark Lutz [452]
If you don’t want the default message and shutdown, you can code try/except statements to catch and recover from exceptions that are raised. Once an exception is caught, the exception is terminated and your program continues.
The raise and assert statements can be used to trigger an exception, exactly as if it had been raised by Python itself. In principle, you can also raise an exception by making a programming mistake, but that’s not usually an explicit goal!
The try/finally statement can be used to ensure actions are run after a block of code exits, regardless of whether it raises an exception or not. The with/as statement can also be used to ensure termination actions are run, but only when processing object types that support it.
Chapter 33. Exception Coding Details
In the prior chapter we took a quick look at exception-related statements in action. Here, we’re going to dig a bit deeper—this chapter provides a more formal introduction to exception processing syntax in Python. Specifically, we’ll explore the details behind the try, raise, assert, and with statements. As we’ll see, although these statements are mostly straightforward, they offer powerful tools for dealing with exceptions in Python code.
* * *
Note
One procedural note up front: The exception story has changed in major ways in recent years. As of Python 2.5, the finally clause can appear in the same try statement as except and else clauses (previously, they could not be combined). Also, as of Python 3.0 and 2.6, the new with context manager statement has become official, and user-defined exceptions must now be coded as class instances, which should inherit from a built-in exception superclass. Moreover, 3.0 sports slightly modified syntax for the raise statement and except clauses. I will focus on the state of exceptions in Python 2.6 and 3.0 in this edition, but because you are still very likely to see the original techniques in code for some time to come, along the way I’ll point out how things have evolved in this domain.
* * *
The try/except/else Statement
Now that we’ve seen the basics, it’s time for the details. In the following discussion, I’ll first present try/except/else and try/finally as separate statements, because in versions of Python prior to 2.5 they serve distinct roles and cannot be combined. As mentioned in the preceding note, in Python 2.5 and later except and finally can be mixed in a single try statement; I’ll explain the implications of this change after we’ve explored the two original forms in isolation.
The try is a compound statement; its most complete form is sketched below. It starts with a try header line, followed by a block of (usually) indented statements, then one or more except clauses that identify exceptions to be caught, and an optional else clause at the end. The words try, except, and else are associated by indenting them to the same level (i.e., lining them up vertically). For reference, here’s the general format in Python 3.0:
try:
except except (name2, name3): except except: else: In this statement, the block under the try header represents the main action of the statement—the code you’re trying to run. The except clauses define handlers for exceptions raised during the try block, and the else clause (if coded) provides a handler to be run if no exceptions occur. The entry here has to do with a feature of raise statements and exception classes, which we will discuss later in this chapter. Here’s how try statements work. When a try statement is entered, Python marks the current program context so it can return to it if an exception occurs. The statements nested under the try header are run first. What