Built-in Exceptions - Page 3
September 14, 2001
Table 5.1
| Exception | Description |
Exception | The root of all exceptions
|
SystemExit | Generated by sys.exit()
|
StandardError | Base for all built-in exceptions
|
ArithmeticError | Base for arithmetic exceptions
|
FloatingPointError | Failure of a floating-point operation
|
OverflowError | Arithmetic overflow
|
ZeroDivisionError | Division or modulus operation with 0
|
AssertionError | Raised by the assert statement
|
AttributeError | Raised when an attribute name is invalid
|
EnvironmentError | Errors that occur externally to Python
|
IOError | I/O or file-related error
|
OSError | Operating system error
|
WindowsError | Error in Windows
|
EOFError | Raised when the end of the file is reached
|
ImportError | Failure of the import statement
|
KeyboardInterrupt | Generated by the interrupt key (usually Ctrl+C)
|
LookupError | Indexing and key errors
|
IndexError | Out-of-range sequence offset
|
KeyError | Nonexistent dictionary key
|
MemoryError | Out of memory
|
NameError | Failure to find a local or global name
|
UnboundLocalError | Unbound local variable
|
RuntimeError | A generic catch-all error
|
NotImplementedError | Unimplemented feature
|
SyntaxError | Parsing error
|
TabError | Inconsistent tab usage (generated with -tt option)
|
IndentationError | Indentation error
|
SystemError | Nonfatal system error in the interpreter
|
TypeError | Passing an inappropriate type to an operation
|
ValueError | Inappropriate or missing value
|
UnicodeError | Unicode encoding error
|
The try statement also supports an else clause,
which must follow the last except clause. This code
is executed if the code in the try block doesn't
raise an exception. Here's an example:
try:
f = open('foo','r')
except IOError:
print 'Unable to open foo'
else:
data = f.read()
f.close()
The finally statement defines a cleanup action for code contained
in a try block. For example:
f = open('foo','r')
try:
#Do some stuff
...
finally:
f.close()
print "File closed regardless of what happened."
The finally clause isn't used to catch errors.
Rather, it's used to provide code that must always be executed,
regardless of whether an error occurs. If no exception is raised,
the code in the finally clause is executed
immediately after the code in the try block. If an
exception occurs, control is first passed to the first statement
of the finally clause. After this code has executed,
the exception is re-raised to be caught by another exception
handler. The finally and except
statements cannot appear together within a single
try statement.
Python defines the built-in exceptions listed in Table 5.1. (For
specific details about these exceptions, see Appendix A.)
All the exceptions in a particular group can be caught by
specifying the group name in an except clause. For
example,
try:
statements
except LookupError: # Catch IndexError or KeyError
statements
or
try:
statements
except StandardError: # Catch any built-in exception
statements
Exceptions - Page 2
Python Essential Reference, Second Edition
Defining New Exceptions - Page 4
|