maroph icon

Python 3: get a hierarchy of all predefined exceptions

maroph | PRO | 05/18/17 01:48:14 PM UTC | 0 ⭐ | 345 👁️ | Never ⏰ | []
Python |

2.36 KB

|

None

|

0 👍

/

0 👎

""" get a hierarchy of all predefined exceptions """
 
 
def class_hierarchy(class_, space=0):
    print(space * ' ' + class_.__name__)
    if not class_ is type:
        for subclass in class_.__subclasses__():
            class_hierarchy(subclass, space + 4)
 
 
if __name__ == '__main__':
    class_hierarchy(BaseException)
 
# ============================================================
# the output should be similar to the following lines
# ============================================================
 
"""
BaseException
    Exception
        EOFError
        NameError
            UnboundLocalError
        ReferenceError
        ValueError
            UnicodeError
                UnicodeEncodeError
                UnicodeDecodeError
                UnicodeTranslateError
            UnsupportedOperation
        AssertionError
        ImportError
            ZipImportError
        Warning
            ImportWarning
            DeprecationWarning
            RuntimeWarning
            ResourceWarning
            PendingDeprecationWarning
            UserWarning
            SyntaxWarning
            FutureWarning
            UnicodeWarning
            BytesWarning
        BufferError
        OSError
            TimeoutError
            InterruptedError
            ProcessLookupError
            NotADirectoryError
            PermissionError
            FileNotFoundError
            ChildProcessError
            ConnectionError
                BrokenPipeError
                ConnectionAbortedError
                ConnectionRefusedError
                ConnectionResetError
            IsADirectoryError
            UnsupportedOperation
            FileExistsError
            BlockingIOError
        AttributeError
        SyntaxError
            IndentationError
                TabError
        StopIteration
        LookupError
            IndexError
            KeyError
            CodecRegistryError
        TypeError
        MemoryError
        SystemError
            CodecRegistryError
        StopAsyncIteration
        ArithmeticError
            FloatingPointError
            OverflowError
            ZeroDivisionError
        RuntimeError
            RecursionError
            NotImplementedError
            _DeadlockError
        Error
    GeneratorExit
    SystemExit
    KeyboardInterrupt
"""

Comments