Exception class hierarchy

When an exception occurs, it starts at the innermost level possible (a child) and travels upward (through the parents), waiting to be caught. This means a couple of things to a programmer:

  • If you don't know what exception may occur, you can always just catch a higher-level exception. For example, if you didn't know that ZeroDivisionError from the preceding exception example was its own exception, you could have used the ArithmeticError for the exception and caught that. The following exceptions list shows that ZeroDivisionError is a child of ArithmeticError, which in turn is a child of Exception, which in turn is a child of BaseException, which is the default class that all other exceptions derive from.
  • Multiple exceptions can be treated the same way. Following on with the preceding example, suppose you plan on using ZeroDivisionError and you want to include FloatingPointError. If you wanted to have the same action taken for both errors, simply catch the parent exception, ArithmeticError. That way, when either a floating-point or a zero-division error occurs, you don't have to have a separate case for each one. Naturally, if you have a need or desire to catch each one separately, perhaps because you want different actions to be taken, then writing exceptions for each case is fine. Also, catching the parent exception means all children exceptions of that parent, even ones you didn't intend to catch.

The following exceptions list shows the hierarchy of exceptions from the Python Library Reference, for Python 3.6. One thing to note is that this hierarchy sometimes changes between Python versions; for example, ArithmeticError is a child of StandardError in version 2.7, but is a child of Exception in version 3.6:

BaseException 
 +-- SystemExit 
 +-- KeyboardInterrupt 
 +-- GeneratorExit 
 +-- Exception 
      +-- StopIteration 
      +-- StopAsyncIteration 
      +-- ArithmeticError 
      |    +-- FloatingPointError 
      |    +-- OverflowError 
      |    +-- ZeroDivisionError 
      +-- AssertionError 
      +-- AttributeError 
      +-- BufferError 
      +-- EOFError 
      +-- ImportError 
      |    +-- ModuleNotFoundError 
      +-- LookupError 
      |    +-- IndexError 
      |    +-- KeyError 
      +-- MemoryError 
      +-- NameError 
      |    +-- UnboundLocalError 
      +-- OSError 
      |    +-- BlockingIOError 
      |    +-- ChildProcessError 
      |    +-- ConnectionError 
      |    |    +-- BrokenPipeError 
      |    |    +-- ConnectionAbortedError 
      |    |    +-- ConnectionRefusedError 
      |    |    +-- ConnectionResetError 
      |    +-- FileExistsError 
      |    +-- FileNotFoundError 
      |    +-- InterruptedError 
      |    +-- IsADirectoryError 
      |    +-- NotADirectoryError 
      |    +-- PermissionError 
      |    +-- ProcessLookupError 
      |    +-- TimeoutError 
      +-- ReferenceError 
      +-- RuntimeError 
      |    +-- NotImplementedError 
      |    +-- RecursionError 
      +-- SyntaxError 
      |    +-- IndentationError 
      |         +-- TabError 
      +-- SystemError 
      +-- TypeError 
      +-- ValueError 
      |    +-- UnicodeError 
      |         +-- UnicodeDecodeError 
      |         +-- UnicodeEncodeError 
      |         +-- UnicodeTranslateError 
      +-- Warning 
           +-- DeprecationWarning 
           +-- PendingDeprecationWarning 
           +-- RuntimeWarning 
           +-- SyntaxWarning 
           +-- UserWarning 
           +-- FutureWarning 
           +-- ImportWarning 
           +-- UnicodeWarning 
           +-- BytesWarning 
           +-- ResourceWarning 

This hierarchy is important to understand when you're writing your code; an exception may not be available depending on which Python version you're using. If you're trying to make your programs as portable as possible, you have to be cognizant of which Python version the target system is running.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21