Skip to content

Instantly share code, notes, and snippets.

@ajschumacher
Last active May 26, 2021 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajschumacher/560d703ca9d0f3bf48a06562d62a7f02 to your computer and use it in GitHub Desktop.
Save ajschumacher/560d703ca9d0f3bf48a06562d62a7f02 to your computer and use it in GitHub Desktop.
Python 3.7.4 exception hierarchy
# inspired by and based on:
# https://julien.danjou.info/python-exceptions-guide/
# https://github.com/jd/julien.danjou.info/blob/master/bin/generate-python-exceptions-graph.py
import builtins
edges = set()
synonyms = {}
for name in dir(builtins):
item = getattr(builtins, name)
if isinstance(item, type) and BaseException in item.__mro__:
assert len(item.__bases__) == 1
edges.add((item.__bases__[0].__name__, item.__name__))
if item.__name__ != name:
synonyms.setdefault(item.__name__, []).append(name)
edges = sorted(edges)
def show(name, level=0, maxlevel=999):
if level > maxlevel:
return
extra = ''
if name in synonyms:
extra = ' aka ' + ' aka '.join(sorted(synonyms[name]))
print(' ' * level + ' * ' + name + extra)
for child in [child for parent, child in edges if parent == name]:
show(child, level + 1, maxlevel=maxlevel)
show('object')
show('Exception', maxlevel=1)
  • object
    • BaseException
      • Exception
        • ArithmeticError
          • FloatingPointError
          • OverflowError
          • ZeroDivisionError
        • AssertionError
        • AttributeError
        • BufferError
        • EOFError
        • ImportError
          • ModuleNotFoundError
        • LookupError
          • IndexError
          • KeyError
        • MemoryError
        • NameError
          • UnboundLocalError
        • OSError aka EnvironmentError aka IOError
          • BlockingIOError
          • ChildProcessError
          • ConnectionError
            • BrokenPipeError
            • ConnectionAbortedError
            • ConnectionRefusedError
            • ConnectionResetError
          • FileExistsError
          • FileNotFoundError
          • InterruptedError
          • IsADirectoryError
          • NotADirectoryError
          • PermissionError
          • ProcessLookupError
          • TimeoutError
        • ReferenceError
        • RuntimeError
          • NotImplementedError
          • RecursionError
        • StopAsyncIteration
        • StopIteration
        • SyntaxError
          • IndentationError
            • TabError
        • SystemError
        • TypeError
        • ValueError
          • UnicodeError
            • UnicodeDecodeError
            • UnicodeEncodeError
            • UnicodeTranslateError
        • Warning
          • BytesWarning
          • DeprecationWarning
          • FutureWarning
          • ImportWarning
          • PendingDeprecationWarning
          • ResourceWarning
          • RuntimeWarning
          • SyntaxWarning
          • UnicodeWarning
          • UserWarning
      • GeneratorExit
      • KeyboardInterrupt
      • SystemExit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment