Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Print Python Exception/Error Hierarchy

ใช้ inspect.getclasstree() และ __subclasses__()

ตัวอย่าง

import inspect

print("The class hierarchy for built-in exceptions is:")
inspect.getclasstree(inspect.getmro(BaseException))

def classtree(cls, indent=0):
    print('.' * indent, cls.__name__)
    for subcls in cls.__subclasses__():
        classtree(subcls, indent + 3)

classtree(BaseException)

ผลลัพธ์ (บางส่วน)

BaseException
... Exception
...... StandardError
......... TypeError
......... ImportError
.......... ZipImportError
......... EnvironmentError
.......... IOError
.......... OSError
............. WindowsError
......... EOFError
......... RuntimeError
.......... NotImplementedError
......... NameError
.......... UnboundLocalError
......... AttributeError
......... SyntaxError
.......... IndentationError
............. TabError
......... LookupError
.......... IndexError
.......... KeyError
.......... CodecRegistryError
......... ValueError
.......... UnicodeError
............. UnicodeEncodeError
............. UnicodeDecodeError
............. UnicodeTranslateError
......... AssertionError
......... ArithmeticError
.......... FloatingPointError
.......... OverflowError
.......... ZeroDivisionError
......... SystemError
......... MemoryError
......... BufferError
...... StopIteration
...... Warning
......... UserWarning
......... DeprecationWarning
......... PendingDeprecationWarning
......... SyntaxWarning
......... RuntimeWarning
......... FutureWarning
......... ImportWarning
......... UnicodeWarning
......... BytesWarning
...... GeneratorExit
...... SystemExit
...... KeyboardInterrupt