簡體   English   中英

是否有一種簡單的方法可以自定義 python 中的 try-except 錯誤代碼輸出?

[英]Is there an easy way to customize try-except error code outputs in python?

我正在嘗試在我正在編寫的腳本中自定義我的錯誤代碼,我想知道我是否可以調整 output。 這是場景。 運行此腳本:

while True:
    try:
        x = "Hello World"
        int(x)
    except (ValueError, RuntimeError, TypeError, NameError, SystemExit):

        sys.exit("Error: Check Section 1.0")

創建一個 output :

An exception has occurred, use %tb to see the full traceback.
SystemExit: Error: Check Section 1.0

有沒有一種簡單的方法,當發生錯誤時,腳本停止並且只說:

Error: Check Section 1.0

試試這個,這個方法直接向您顯示“錯誤:檢查第 1.0 節”。

while True:
    try:
       x = "Hello World"
       int(x)
    except:
       sys.exit("Error: Check Section 1.0")

只需printbreak ,因為您將其保存在循環中

while True:
    try:
        x = "Hello World"
        int(x)
    except:
        print("Error: Check Section 1.0")
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM