Exceptions
Try ... except
try:
suite
[except [exception [, value]:
suite]+
[else:
suite]
- If no exception is raised, the "
else"
suite is run
immediately after the "try" suite (unless the "try"
suite exits with a return, break,
or continue statement).
- Can specify a tuple of exceptions:
except
(KeyError,
NameError)
Try ... finally
try:
suite
finally:
suite
- The "
finally" suite is
guaranteed to be run immediately after
the "try" suite.
- If the "
try" suite raised an
exception, the "finally" suite
will re-raise it.
Raise statement
raise exceptionInstance # Use an existing exception instance (object)
raise exceptionClass [,value [, traceback-object]] # Create new exception instance
raise
- A raise statement without arguments
re-raises the last
exception raised in the current function.
Assert statement
assert expr [, message]
- If the expression evaluates to
false,
an AssertionError
exception is raised.
- Assertions are disabled if
__debug__
is
0.