如何编写打印Python异常/错误层次的代码
在本教程中,我们将了解如何编写一个打印Python错误层次结构的代码。但在开始之前,我们应该先学习什么是异常?异常是即使我们的代码在语法上正确,也可能发生的错误。这些错误并非绝对致命,用户在执行代码时遇到的错误被称为异常。Python中有许多内置的异常,我们将在这里尝试打印出它们的层次结构。
要打印树状层次结构,我们可以使用Python的inspect模块。inspect模块用于获取有关对象(如模块、类方法、函数和程序的对象)的信息。例如,用户可以使用它来检查类的内容,提取和格式化函数的参数列表。
要构建一个树状层次结构,我们可以使用inspect.getclasstree()函数。
语法:
inspect.getclasstree(classes, unique = False)
inspect.getclasstree(): 用于将给定的类列表按层次结构排列成嵌套列表。嵌套列表出现的地方将包含从该列表前面的类派生的类。
示例:
# For printing the hierarchy for inbuilt exceptions:
# First, we will import the inspect module
import inspect as ipt
# Then we will create tree_class function
def tree_class(cls, ind = 0):
# Then we will print the name of the class
print ('-' * ind, cls.__name__)
# now, we will iterate through the subclasses
for K in cls.__subclasses__():
tree_class(K, ind + 3)
print ("The Hierarchy for inbuilt exceptions is: ")
# THE inspect.getmro() will return the tuple
# of class which is cls's base classes.
#Now, we will build a tree hierarchy
ipt.getclasstree(ipt.getmro(BaseException))
# function call
tree_class(BaseException)
输出:
The Hierarchy for inbuilt exceptions is:
BaseException
---> Exception
------> TypeError
---------> MultipartConversionError
---------> FloatOperation
------> StopAsyncIteration
------> StopIteration
------> ImportError
---------> ModuleNotFoundError
---------> ZipImportError
------> OSError
---------> ConnectionError
------------> BrokenPipeError
------------> ConnectionAbortedError
------------> ConnectionRefusedError
------------> ConnectionResetError
--------------> RemoteDisconnected
---------> BlockingIOError
---------> ChildProcessError
---------> FileExistsError
---------> FileNotFoundError
---------> IsADirectoryError
---------> NotADirectoryError
---------> InterruptedError
------------> InterruptedSystemCall
---------> PermissionError
---------> ProcessLookupError
---------> TimeoutError
---------> UnsupportedOperation
---------> Error
------------> SameFileError
---------> SpecialFileError
---------> ExecError
---------> ReadError
---------> herror
---------> gaierror
---------> timeout
---------> SSLError
------------> SSLCertVerificationError
------------> SSLZeroReturnError
------------> SSLWantReadError
------------> SSLWantWriteError
------------> SSLSyscallError
------------> SSLEOFError
---------> URLError
------------> HTTPError
------------> ContentTooShortError
------> EOFError
---------> IncompleteReadError
------> RuntimeError
---------> RecursionError
---------> NotImplementedError
------------> StdinNotImplementedError
------------> ZMQVersionError
---------> _DeadlockError
---------> BrokenBarrierError
---------> BrokenExecutor
---------> SendfileNotAvailableError
------> NameError
---------> UnboundLocalError
------> AttributeError
---------> FrozenInstanceError
------> SyntaxError
---------> IndentationError
------------> TabError
------> LookupError
---------> IndexError
---------> KeyError
------------> UnknownBackend
------------> NoSuchKernel
.
.
.
.
---> GeneratorExit
---> SystemExit
---> KeyboardInterrupt
---> CancelledError
结论
在本教程中,我们讨论了如何使用Python的inspect模块打印异常错误的层次结构。