The purpose of a “try”
construct is to specify what actions to take in the event
of errors. For an introduction to Python's exception
mechanism, see Section 18, “Exceptions: Error signaling and handling”.
When an exception is raised, two items are associated with it:
An exception type, and
an exception value.
Here is the most general form of a try
construct. Symbols like and B0 represent indented blocks of statements. Each
B1except clause names some exception class
, and optionally a variable Ei that will be set to the exception value.
vi
try:
B0
except E1[, v1]:
B1
except E2[, v2]:
B2
except ...:
...
else:
Be
finally:
Bf
The else: and finally:
blocks are optional. There must be at least one except block, but there may be any number.
Here is a simplified description of the execution of a
try block in general:
If executes without raising any
exceptions, the B0else block is executed, then the Befinally block .
Bf
If the execution of block
raises some exception with type B0,
that type is compared to all the exception
classes E0, E1, ... named in the various
E2except clauses.
The criterion for matching is that must be the same class as,
or some subclass of, one of the class names E0 mentioned in an Eiexcept clause. If there are multiple
matches, the first except clause block
is executed. In this case,
we say that the exception has been caught (or handled).
Bi
When an except block matches and is
entered, the corresponding variable is set to the value of the
exception.
vi
If there is a finally clause, it is
executed, whether the exception was caught or not.
If the exception was not caught, it is re-raised
after the end of the finally clause.
Examples:
>>> try: ... raise ValueError, "Your arm's off." ... except IOError, detail: ... print "I/O error caught:", detail ... except ValueError, detail: ... print "Value error caught:", detail ... except SyntaxError, detail: ... print "Syntax error caught:", detail ... else: ... print "This is the else clause" ... finally: ... print "This is the finally clause" ... Value error caught: Your arm's off. This is the finally clause >>> try: ... raise ValueError, "Uncaught!" ... except IOError, detail: ... print "I/O error:", detail ... else: ... print "This is the else clause" ... finally: ... print "This is the finally clause" ... This is the finally clause Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: Uncaught! >>> try: ... print "No exceptions raised" ... except ValueError, x: ... print "ValueError:", x ... else: ... print "This is the else clause" ... finally: ... print "This is the finally clause" ... No exceptions raised This is the else clause This is the finally clause
For those of you who are interested in the gory details,
the fun begins when a second or even a third exception is
raised inside an except, else, or finally clause. The results are
well-defined, and here is a pseudocode description of the
edge cases. In this procedure, we'll use two internal
variables named pending and detail.
Set pending to None.
Attempt to execute block .
If this block raises an exception B0
with detail E0, set d0pending to and set E0detail to .
d0
If pending is None, go
to Step 8.
Find the first block except such that Ei,
vi:issubclass(.
E0, Ei )
If there is no such match, go to Step 10.
Set to videtail.
Attempt to execute block .
Bi
If this block raises some new exception with detail En, set dnpending
to and set Endetail to .
dn
However, if block executes
without exception, set Bipending to
None. In this case, the original
exception is said to have been caught or handled.
Go to Step 10.
If there is no else: clause, go to
Step 10.
Attempt to execute the else: block
.
Be
If this block raises some new exception with detail En, set dnpending
to and set Endetail to .
dn
If there is no finally: clause,
proceed to Step 12.
Attempt to execute the finally: block
.
Ef
If this block raises some new exception with detail En, set dnpending
to and set Endetail to .
dn
If pending is not None,
re-raise the exception as in this statement:
raise pending, detail
If pending is None,
fall through to the statement following the try: block.