The try statement specifies exception handlers and/or cleanup code for a group of statements:
try_stmt: try_exc_stmt | try_fin_stmt try_exc_stmt: "try" ":" suite ("except" [condition ["," target]] ":" suite)+ ["else" ":" suite] try_fin_stmt: "try" ":" suite "finally" ":" suite
There are two forms of try statement: try...except and try...finally. These forms cannot be mixed.
The try...except form specifies one or more exception handlers
(the except clauses). 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 inspects the except clauses in turn until
one is found that matches the exception. A condition-less except
clause, if present, must be last; it matches any exception. For an
except clause with a condition, that condition 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 either the object that identifies the exception or it is a tuple
containing an item that is compatible with the exception. Note that
the object identities must match, i.e. it must be the same object, not
just an object 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.
If the evaluation of a condition in the header of an except clause raises an exception, the original search for a handler is cancelled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire try statement raised the exception).
When a matching except clause is found, the exception's parameter is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. When the end of this suite 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 an except clause's suite is executed, details about the
exception are assigned to three variables in the sys module:
sys.exc_type receives the object identifying the exception;
sys.exc_value receives the exception's parameter;
sys.exc_traceback receives a traceback object (see section
) identifying the point in the program where the
exception occurred.
The optional else clause is executed when no exception occurs
in the try clause. Exceptions in the else clause are
not handled by the preceding except clauses.
The try...finally form specifies a `cleanup' handler. The
try clause is executed. When no exception occurs, the
finally clause is executed. When an exception occurs in the
try clause, the exception is temporarily saved, the
finally clause is executed, and then the saved exception is
re-raised. If the finally clause raises another exception or
executes a return, break or continue statement,
the saved exception is lost.
When a return or break statement is executed in the
try suite of a try...finally statement, the
finally clause is also executed `on the way out'. A
continue statement is illegal in the try clause. (The
reason is a problem with the current implementation - this
restriction may be lifted in the future).