如何編寫列印 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 模塊列印層次結構中的異常錯誤。


原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/278321.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-19 13:21
下一篇 2024-12-19 13:21

相關推薦

發表回復

登錄後才能評論