170 likes | 223 Views
Python Built-in Exceptions. Data Fusion Albert Esterline. Source: http://docs.python.org/library/exceptions.html For the exception hierarchy, see the bottom of the page, accessible directly at http://docs.python.org/library/exceptions.html#exception-hierarchy.
E N D
Python Built-in Exceptions Data Fusion Albert Esterline Source: http://docs.python.org/library/exceptions.html For the exception hierarchy, see the bottom of the page, accessible directly at http://docs.python.org/library/exceptions.html#exception-hierarchy
The class hierarchy for built-in exceptions(See module exceptions) BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StandardError | +-- BufferError | +-- ArithmeticError | | +-- FloatingPointError | | +-- OverflowError | | +-- ZeroDivisionError | +-- AssertionError | +-- AttributeError | +-- EnvironmentError | | +-- IOError | | +-- OSError | | +-- WindowsError (Windows) | | +-- VMSError (VMS)
+-- StandardError (cont.) | +-- EOFError | +-- ImportError | +-- LookupError | | +-- IndexError | | +-- KeyError | +-- MemoryError | +-- NameError | | +-- UnboundLocalError | +-- ReferenceError | +-- RuntimeError | | +-- NotImplementedError | +-- SyntaxError | | +-- IndentationError | | +-- TabError | +-- SystemError | +-- TypeError | +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError
+-- Exception (cont.) +-- Warning +-- DeprecationWarning +-- PendingDeprecationWarning +-- RuntimeWarning +-- SyntaxWarning +-- UserWarning +-- FutureWarning +-- ImportWarning +-- UnicodeWarning +-- BytesWarning
Exceptions used only as base classes for other exceptions BaseException • Base class for all built-in exceptions • Not meant to be directly inherited by user-defined classes Exception • All built-in, non-system-exiting exceptions are derived from this class • All user-defined exceptions should also be derived from this class StandardError • Base class for all built-in exceptions except StopIteration, GeneratorExit, KeyboardInterrupt and SystemExit • Derived from Exception ArithmeticError • Base class for built-in exceptions raised for arithmetic errors
LookupError • Base class for exceptions raised when a key or index used on a dictionary or sequence is invalid EnvironmentError • Base class for exceptions that can occur outside the Python system: IOError, OSError • When these exceptions are created with a 2-tuple, • 1st item is available on the instance’s errno attribute (assumed to be an error number), and • 2nd is available on the strerror attribute (usually the associated error message) • The tuple itself is also available on the args attribute • When such an exception is instantiated with a 3-tuple, • 1st 2 items are available as above • 3rd is available on the filename attribute
Exceptions that are those actually raised AssertionError • Raised when an assert statement fails. AttributeError • Raised when an attribute reference or assignment fails • When an object doesn’t support attribute references or attribute assignments, TypeError is raised EOFError • Raised when built-in function input() or raw_input() hits an EOF without reading any data • Methods file.read() and file.readline() return an empty string when they hit EOF FloatingPointError • Raised when a floating point operation fails
GeneratorExit • Raise when a generator‘s close() method is called • Directly inherits from BaseException instead of StandardError since it’s technically not an error IOError • Raised when an I/O operation fails for an I/O-related reason • Derived from EnvironmentError ImportError • Raised when an import statement fails to find the module definition or when a from ... import fails to find a name to be imported IndexError • Raised when a sequence subscript is out of range KeyError • Raised when a dictionary key isn’t found
KeyboardInterrupt • Raised when user hits the interrupt key (normally Control-C or Delete) • Inherits from BaseException, so not accidentally caught by code that catches Exception MemoryError • Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects) • Associated value is a string indicating what kind of (internal) operation ran out of memory NameError • Raised when a local or global name isn’t found • Associated value is a message that includes the name not found NotImplementedError • Derived from RuntimeError • In user defined base classes, abstract methods should raise this when they require derived classes to override the method
OSError • Derived from EnvironmentError • Raised when a function returns a system-related error • The errno attribute is a numeric error code • The strerror attribute is the corresponding string • For exceptions involving a file system path (e.g., chdir()), the instance contains a 3rd attribute: filename, the file name passed to the function OverflowError • Raised when the result of an arithmetic operation is too large RuntimeError • Raised when an error doesn’t fall in any other category • Associated value is a string indicating the precisely problem • Mostly a relic
StopIteration • Raised by an iterator‘s next() method to signal no further values • Derived from Exception rather than StandardError: not really an error SyntaxError • Raised when the parser encounters a syntax error • May occur • in an import statement, • in an exec statement, • in a call to the built-in function eval() or input(), or • when reading the initial script or standard input (also interactively) • Instances have attributes filename, lineno, offset, and text SystemError • Raised when the interpreter finds an internal error, but there’s still hope • Associated value is a string indicating what went wrong (in low-level terms). • Report this to the maintainer of your Python interpreter
SystemExit • Raised by the sys.exit() function • If the associated value is a plain integer, it specifies the system exit status (passed to C’s exit() function) • If it’s None, exit status is 0 • If it has another type (e.g., a string), the object’s value is printed and the exit status is 1 • A call to sys.exit() is translated into an exception so that • finally clauses can be executed, and • a debugger can execute a script without losing control • Inherits from BaseException instead of StandardError or Exception so it isn’t accidentally caught by code catching Exception
TypeError • Raised when an operation/function applied to an object of inappropriate type • Associated value is a string giving details about the type mismatch UnboundLocalError • Raised when a reference is made to a local variable in a function or method, but no value bound to that variable • Subclass of NameError UnicodeError • Raised when a Unicode-related encoding or decoding error occurs • Subclass of ValueError UnicodeEncodeError • Raised when a Unicode-related error occurs during encoding • Subclass of UnicodeError UnicodeDecodeError • Raised when a Unicode-related error occurs during decoding • Subclass of UnicodeError
UnicodeTranslateError • Raised when a Unicode-related error occurs during translating • Subclass of UnicodeError ValueError • Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value WindowsError • Raised when a Windows-specific error occurs or when the error number doesn’tt correspond to an errno value • Subclass of OSError ZeroDivisionError • Raised when the 2nd argument of a division or modulo operation is 0 • Associated value is a string indicating the type of the operands and the operation
Exceptions used as warning categories Warning • Base class for warning categories UserWarning • Base class for warnings generated by user code DeprecationWarning • Base class for warnings about deprecated features PendingDeprecationWarning • Base class for warnings about features to be deprecated in the future SyntaxWarning • Base class for warnings about dubious syntax
RuntimeWarning • Base class for warnings about dubious runtime behavior FutureWarning • Base class for warnings about constructs that will change semantically in the future ImportWarning • Base class for warnings about probable mistakes in module imports UnicodeWarning • Base class for warnings related to Unicode