The try statement

The try statement specifies exception handlers and/or cleanup code for a group of statements:

Syntax (simplified):

try:
    suite
[except [expression [, target]] :
    suite ]…
[else:
    suite ]
[finally:
    suite ]

Changed in version 2.5: In previous versions of Python, try…except…finally did not work. try…except had to be nested in try…finally.

The 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,