maroph

Python 3: get a hierarchy of all predefined exceptions

May 18th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. """ get a hierarchy of all predefined exceptions """
  2.  
  3.  
  4. def class_hierarchy(class_, space=0):
  5.     print(space * ' ' + class_.__name__)
  6.     if not class_ is type:
  7.         for subclass in class_.__subclasses__():
  8.             class_hierarchy(subclass, space + 4)
  9.  
  10.  
  11. if __name__ == '__main__':
  12.     class_hierarchy(BaseException)
  13.  
  14. # ============================================================
  15. # the output should be similar to the following lines
  16. # ============================================================
  17.  
  18. """
  19. BaseException
  20.    Exception
  21.        EOFError
  22.        NameError
  23.            UnboundLocalError
  24.        ReferenceError
  25.        ValueError
  26.            UnicodeError
  27.                UnicodeEncodeError
  28.                UnicodeDecodeError
  29.                UnicodeTranslateError
  30.            UnsupportedOperation
  31.        AssertionError
  32.        ImportError
  33.            ZipImportError
  34.        Warning
  35.            ImportWarning
  36.            DeprecationWarning
  37.            RuntimeWarning
  38.            ResourceWarning
  39.            PendingDeprecationWarning
  40.            UserWarning
  41.            SyntaxWarning
  42.            FutureWarning
  43.            UnicodeWarning
  44.            BytesWarning
  45.        BufferError
  46.        OSError
  47.            TimeoutError
  48.            InterruptedError
  49.            ProcessLookupError
  50.            NotADirectoryError
  51.            PermissionError
  52.            FileNotFoundError
  53.            ChildProcessError
  54.            ConnectionError
  55.                BrokenPipeError
  56.                ConnectionAbortedError
  57.                ConnectionRefusedError
  58.                ConnectionResetError
  59.            IsADirectoryError
  60.            UnsupportedOperation
  61.            FileExistsError
  62.            BlockingIOError
  63.        AttributeError
  64.        SyntaxError
  65.            IndentationError
  66.                TabError
  67.        StopIteration
  68.        LookupError
  69.            IndexError
  70.            KeyError
  71.            CodecRegistryError
  72.        TypeError
  73.        MemoryError
  74.        SystemError
  75.            CodecRegistryError
  76.        StopAsyncIteration
  77.        ArithmeticError
  78.            FloatingPointError
  79.            OverflowError
  80.            ZeroDivisionError
  81.        RuntimeError
  82.            RecursionError
  83.            NotImplementedError
  84.            _DeadlockError
  85.        Error
  86.    GeneratorExit
  87.    SystemExit
  88.    KeyboardInterrupt
  89. """
Advertisement
Add Comment
Please, Sign In to add comment