如何打印Python异常/错误层次结构?
我们导入检查模块,并专门使用该getclasstree()函数来打印python异常/错误层次结构。
此代码将异常类的给定列表排列并打印到嵌套列表的层次结构中。我们通过一个继承树递归地遍历__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)
输出结果
运行代码后,我们得到以下输出。
The class hierarchy for built-in exceptions is: 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 ............ CodecRegistryError ......... ReferenceError ......... MemoryError ......... BufferError ...... StopIteration ...... Warning ......... UserWarning ......... DeprecationWarning ......... PendingDeprecationWarning ......... SyntaxWarning ......... RuntimeWarning ......... FutureWarning ......... ImportWarning ......... UnicodeWarning ......... BytesWarning ...... _OptionError ...... error ...... Error ...... TokenError ...... StopTokenizing ...... error ...... EndOfBlock ... GeneratorExit ... SystemExit ... KeyboardInterrupt
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短