Learning Python - Mark Lutz [453]
If an exception does occur while the try block’s statements are running, Python jumps back to the try and runs the statements under the first except clause that matches the raised exception. Control resumes below the entire try statement after the except block runs (unless the except block raises another exception).
If an exception happens in the try block and no except clause matches, the exception is propagated up to the last matching try statement that was entered in the program or, if it’s the first such statement, to the top level of the process (in which case Python kills the program and prints a default error message).
If no exception occurs while the statements under the try header run, Python runs the statements under the else line (if present), and control then resumes below the entire try statement.
In other words, except clauses catch any exceptions that happen while the try block is running, and the else clause runs only if no exceptions happen while the try block runs.
except clauses are focused exception handlers—they catch exceptions that occur only within the statements in the associated try block. However, as the try block’s statements can call functions coded elsewhere in a program, the source of an exception may be outside the try statement itself. I’ll have more to say about this when we explore try nesting in Chapter 35.
try Statement Clauses
When you write a try statement, a variety of clauses can appear after the try header. Table 33-1 summarizes all the possible forms—you must use at least one. We’ve already met some of these: as you know, except clauses catch exceptions, finally clauses run on the way out, and else clauses run if no exceptions are encountered.
Syntactically, there may be any number of except clauses, but you can code else only if there is at least one except, and there can be only one else and one finally. Through Python 2.4, the finally clause must appear alone (without else or except); the try/finally is really a different statement. As of Python 2.5, however, a finally can appear in the same statement as except and else (more on the ordering rules later in this chapter when we meet the unified try statement).
Table 33-1. try statement clause forms
Clause form
Interpretation
except:
Catch all (or all other) exception types.
except name:
Catch a specific exception only.
except name as value:
Catch the listed exception and its instance.
except (name1, name2):
Catch any of the listed exceptions.
except (name1, name2) as value:
Catch any listed exception and its instance.
else:
Run if no exceptions are raised.
finally:
Always perform this block.
We’ll explore the entries with the extra as value part when we meet the raise statement. They provide access to the objects that are raised as exceptions.
The first and fourth entries in Table 33-1 are new here:
except clauses that list no exception name (except:) catch all exceptions not previously listed in the try statement.
except clauses that list a set of exceptions in parentheses (except (e1, e2, e3):) catch any of the listed exceptions.
Because Python looks for a match within a given try by inspecting the except clauses from top to bottom, the parenthesized version has the same effect as listing each exception in its own except clause, but you have to code the statement body only once. Here’s an example of multiple except clauses at work, which demonstrates just how specific your handlers can be:
try:
action()
except NameError:
...
except IndexError:
...
except KeyError:
...
except (AttributeError, TypeError, SyntaxError):
...
else:
...
In this example, if an exception is raised while the call to the action function is running, Python returns to the try and searches for the first except that names the exception raised. It inspects the except clauses from top to bottom and left to right, and runs the statements under the first