|Python Language Reference Manual|
by Guido van Rossum and Fred L. Drake, Jr.
Paperback (6"x9"), 120 pages
RRP £12.95 ($19.95)
Sales of this book support the Python Software Foundation! Get a printed copy>>>
try statement specifies exception handlers and/or cleanup
code for a group of statements:
try_stmt try1_stmt | try2_stmt
try1_stmt "try" ":"
suite)+ [ "else" ":"
suite] ["finally" ":"
try2_stmt "try" ":"
In versions of Python prior to 2.5,
finally did not work.
except had to be nested in
except clause(s) specify one or more exception handlers.
When no exception occurs in the
try clause, no exception handler is executed. When an
exception occurs in the
try suite, a search for an exception
handler is started. This search inspects the except clauses in turn until
one is found that matches the exception. An expression-less except
clause, if present, must be last; it matches any exception. For an
except clause with an expression, that expression is evaluated, and the
clause matches the exception if the resulting object is "compatible"
with the exception. An object is compatible with an exception if it
is the class or a base class of the exception object, a tuple
containing an item compatible with the exception, or, in the
(deprecated) case of string exceptions, is the raised string itself
(note that the object identities must match, i.e. it must be the same
string object, not just a string with the same value).
If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack.(17)
If the evaluation of an expression in the header of an except clause
raises an exception, the original search for a handler is canceled
and a search starts for the new exception in the surrounding code and
on the call stack (it is treated as if the entire
raised the exception).
When a matching except clause is found, the exception is assigned to the target specified in that except clause, if present, and the suite of the except clause is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.)
Before the suite of an except clause is executed, details about the
exception are assigned to three variables in the
the object identifying the exception;
the exception's parameter;
sys.exc_traceback receives a
(see section 3.2)
identifying the point in the program where the exception occurred.
These details are also available through the
function, which returns a tuple
(exc_type, exc_value, exc_traceback). Use of the corresponding variables is
deprecated in favor of this function, since their use is unsafe in a
threaded program. As of Python 1.5, the variables are restored to
their previous values (before the call) when returning from a function
that handled an exception.
else clause is executed if and when control
flows off the end of the
try clause.(18) Exceptions in the
else clause are not handled by the
finally is present, it specifies a `cleanup' handler. The
try clause is executed, including any
else clauses. If an exception occurs in any of the clauses
and is not handled, the exception is temporarily saved. The
finally clause is executed. If there is a saved exception,
it is re-raised at the end of the
finally clause raises another exception or
break statement, the saved
exception is lost. The exception information is not available to the
program during execution of the
continue statement is
executed in the
try suite of a
finally clause is also executed `on the way out.' A
continue statement is illegal in the
(The reason is a problem with the current implementation--this
restriction may be lifted in the future).
|ISBN 0954161785||Python Language Reference Manual||See the print edition|