我需要查看异常层次结构,并使其显示良好,如here所示。
在终端上,我得到了以字母顺序显示的那些内置异常(以及其他内容):
>>> import pprint
>>> pprint.pprint(dir(__builtins__))
知道BaseException
是所有现有异常的基类,你知道如何实现这个目标吗?
修改
如果需要:How to find all the subclasses of a class given its name?
答案 0 :(得分:3)
def print_classes(cls, indent):
print('{}{}'.format(indent, cls.__name__))
for sc in cls.__subclasses__():
print_classes(sc, indent + " ")
print_classes((BaseException),'')
答案 1 :(得分:2)
这是一个部分解决方案,为您提供了一个源于什么的字典。
为了打造更漂亮的印刷品,您必须自己做一些事情:
import inspect
l = [eval(x) for x in dir(__builtins__) if inspect.isclass(eval(x)) and issubclass(eval(x),BaseException)]
d = {}
for err in l:
d[err] = err.__subclasses__()
import pprint
pprint.pprint(d)
输出:
{<class 'ResourceWarning'>: [],
<class 'BytesWarning'>: [],
<class 'UnicodeWarning'>: [],
<class 'ImportWarning'>: [],
<class 'FutureWarning'>: [],
<class 'RuntimeWarning'>: [],
<class 'SyntaxWarning'>: [],
<class 'PendingDeprecationWarning'>: [],
<class 'DeprecationWarning'>: [],
<class 'UserWarning'>: [],
<class 'Warning'>: [<class 'UserWarning'>,
<class 'DeprecationWarning'>,
<class 'PendingDeprecationWarning'>,
<class 'SyntaxWarning'>,
<class 'RuntimeWarning'>,
<class 'FutureWarning'>,
<class 'ImportWarning'>,
<class 'UnicodeWarning'>,
<class 'BytesWarning'>,
<class 'ResourceWarning'>],
<class 'BufferError'>: [],
<class 'MemoryError'>: [],
<class 'ReferenceError'>: [],
<class 'SystemError'>: [<class 'encodings.CodecRegistryError'>],
<class 'ZeroDivisionError'>: [],
<class 'OverflowError'>: [],
<class 'FloatingPointError'>: [],
<class 'ArithmeticError'>: [<class 'FloatingPointError'>,
<class 'OverflowError'>,
<class 'ZeroDivisionError'>],
<class 'AssertionError'>: [],
<class 'UnicodeTranslateError'>: [],
<class 'UnicodeDecodeError'>: [],
<class 'UnicodeEncodeError'>: [],
<class 'UnicodeError'>: [<class 'UnicodeEncodeError'>,
<class 'UnicodeDecodeError'>,
<class 'UnicodeTranslateError'>],
<class 'ValueError'>: [<class 'UnicodeError'>,
<class 'io.UnsupportedOperation'>],
<class 'KeyError'>: [],
<class 'IndexError'>: [],
<class 'LookupError'>: [<class 'IndexError'>,
<class 'KeyError'>,
<class 'encodings.CodecRegistryError'>],
<class 'TabError'>: [],
<class 'IndentationError'>: [<class 'TabError'>],
<class 'SyntaxError'>: [<class 'IndentationError'>],
<class 'AttributeError'>: [],
<class 'UnboundLocalError'>: [],
<class 'NameError'>: [<class 'UnboundLocalError'>],
<class 'NotImplementedError'>: [],
<class 'RecursionError'>: [],
<class 'RuntimeError'>: [<class 'RecursionError'>,
<class 'NotImplementedError'>,
<class '_frozen_importlib._DeadlockError'>],
<class 'EOFError'>: [],
<class 'TimeoutError'>: [],
<class 'ProcessLookupError'>: [],
<class 'PermissionError'>: [],
<class 'InterruptedError'>: [],
<class 'NotADirectoryError'>: [],
<class 'IsADirectoryError'>: [],
<class 'FileNotFoundError'>: [],
<class 'FileExistsError'>: [],
<class 'ConnectionResetError'>: [],
<class 'ConnectionRefusedError'>: [],
<class 'ConnectionAbortedError'>: [],
<class 'BrokenPipeError'>: [],
<class 'ChildProcessError'>: [],
<class 'ConnectionError'>: [<class 'BrokenPipeError'>,
<class 'ConnectionAbortedError'>,
<class 'ConnectionRefusedError'>,
<class 'ConnectionResetError'>],
<class 'BlockingIOError'>: [],
<class 'OSError'>: [<class 'ConnectionError'>,
<class 'BlockingIOError'>,
<class 'ChildProcessError'>,
<class 'FileExistsError'>,
<class 'FileNotFoundError'>,
<class 'IsADirectoryError'>,
<class 'NotADirectoryError'>,
<class 'InterruptedError'>,
<class 'PermissionError'>,
<class 'ProcessLookupError'>,
<class 'TimeoutError'>,
<class 'io.UnsupportedOperation'>,
<class 'signal.ItimerError'>],
<class 'ModuleNotFoundError'>: [],
<class 'ImportError'>: [<class 'ModuleNotFoundError'>,
<class 'zipimport.ZipImportError'>],
<class 'KeyboardInterrupt'>: [],
<class 'SystemExit'>: [],
<class 'GeneratorExit'>: [],
<class 'StopIteration'>: [],
<class 'StopAsyncIteration'>: [],
<class 'TypeError'>: [],
<class 'Exception'>: [<class 'TypeError'>,
<class 'StopAsyncIteration'>,
<class 'StopIteration'>,
<class 'ImportError'>,
<class 'OSError'>,
<class 'EOFError'>,
<class 'RuntimeError'>,
<class 'NameError'>,
<class 'AttributeError'>,
<class 'SyntaxError'>,
<class 'LookupError'>,
<class 'ValueError'>,
<class 'AssertionError'>,
<class 'ArithmeticError'>,
<class 'SystemError'>,
<class 'ReferenceError'>,
<class 'BufferError'>,
<class 'MemoryError'>,
<class 'Warning'>,
<class 'locale.Error'>,
<class 'warnings._OptionError'>,
<class 'sre_constants.error'>,
<class 'sre_parse.Verbose'>,
<class 'tokenize.TokenError'>,
<class 'tokenize.StopTokenizing'>,
<class 'inspect.EndOfBlock'>],
<class 'BaseException'>: [<class 'Exception'>,
<class 'GeneratorExit'>,
<class 'SystemExit'>,
<class 'KeyboardInterrupt'>]}