1 / 16

Python Built-in Exceptions

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.

mccoyj
Download Presentation

Python Built-in Exceptions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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)

  3. +-- StandardError (cont.) | +-- EOFError | +-- ImportError | +-- LookupError | | +-- IndexError | | +-- KeyError | +-- MemoryError | +-- NameError | | +-- UnboundLocalError | +-- ReferenceError | +-- RuntimeError | | +-- NotImplementedError | +-- SyntaxError | | +-- IndentationError | | +-- TabError | +-- SystemError | +-- TypeError | +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError

  4. +-- Exception (cont.) +-- Warning +-- DeprecationWarning +-- PendingDeprecationWarning +-- RuntimeWarning +-- SyntaxWarning +-- UserWarning +-- FutureWarning +-- ImportWarning +-- UnicodeWarning +-- BytesWarning

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

More Related