预计阅读本页时间:-
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:
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
try:
<statements> # Run this main action first
except <name1>:
<statements> # Run if name1 is raised during try block
except (name2, name3):
<statements> # Run if any of these exceptions occur
except <name4> as <data>:
<statements> # Run if name4 is raised, and get instance raised
except:
<statements> # Run for all (other) exceptions raised
else:
<statements> # Run if no exception was raised during try block
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 <data> 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 happens next depends on whether exceptions are raised while the try block’s statements are running:
- 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.