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 25, “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
(or a tuple of exception
classes), and optionally a variable Ei that
will be set to the exception value.
vi
try:
B0
except E1 [as v1]:
B1
except E2 [as 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 against each E0except clause until one of them matches
the raised exception or there are no more except clauses.
The matching condition is slightly complicated:
for some clause “except ”,
expression Ei as vi: is either an
exception class or a tuple of exception classes.
Ei
If is a single class, it is
considered a match if Ei is either the same class as E0 or a subclass of Ei.
Ei
If is a tuple of exception
classes, the raised exception is compared to each
to see if it is the same class or a subclass, as
in the single-class case.
Ei
If multiple except clauses match, the
first matching clause is said to handle the exception. The
corresponding variable
(if present) is bound to the raised exception
instance, and control passes to the corresponding
block vi.
Bi
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("Strike three!")
... except IOError as x:
... print "I/O error caught:", x
... except ValueError as x:
... print "Value error caught:", x
... except SyntaxError as x:
... print "Syntax error caught:", x
... else:
... print "This is the else clause"
... finally:
... print "This is the finally clause"
...
Value error caught: Strike three!
This is the finally clause
>>> try:
... raise ValueError("Uncaught!")
... except IOError as x:
... print "I/O error:", x
... 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 as 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.