如果我在 Python 中寫了一些東西并且出現了問題,我會自動獲得回溯。
例如:
#!/usr/bin/env python
print("this will raise a division by zero exception")
print(2/0)
它會自動拋出例外的回溯
>>> %Run test.py
this will raise a division by zero exception
Traceback (most recent call last):
File "/home/pi/Desktop/test.py", line 4, in <module>
print(2/0)
ZeroDivisionError: division by zero
>>>
現在,假設我不希望程式崩潰并燒毀,但我想優雅地處理例外——比如我需要關閉檔案或其他什么。
即:
#!/usr/bin/env python
import sys
try:
print("Assume I'm doing something with a resource, like a file")
print(". . . and something goes wrong\n")
print("(This will raise a division by zero exception when I try to \"print(2/0)\")\n")
print(2/0)
except BaseException as e:
print(". . . and I want to handle the exception gracefully:\n")
print("Oops! Something happened! (",e, ")\n")
sys.exit(0)
這讓我得到了預期:
>>> %Run test.py
Assume I'm doing something with a resource, like a file
. . . and something goes wrong
(This will raise a division by zero exception when I try to "print(2/0)")
. . . and I want to handle the exception gracefully:
Oops! Something happened! ( division by zero )
─────────────────────────────────────────────────────────────────────────────────────────────────
Python 3.7.3 (/usr/bin/python3)
>>>
但是,如果我嘗試手動處理例外,我被告知為了獲得系統提供的回溯(錯誤的呼叫堆疊和位置),我必須匯入回溯。
當我這樣做時,我可以獲得我想要的所有資訊。
#!/usr/bin/env python
import sys
import traceback
try:
print("Assume I'm doing something with a resource, like a file")
print(". . . and something goes wrong\n")
print("(This will raise a division by zero exception when I try to \"print(2/0)\")\n")
print(2/0)
except BaseException as e:
print(". . . and I want to handle the exception gracefully:\n")
print("Oops! Something happened! (",e, ")\n")
traceback.print_exc()
sys.exit(0)
和預期的結果:
────────────────────────────────────────────────────────────────────────────────────────────────
Python 3.7.3 (/usr/bin/python3)
>>> %Run test.py
Assume I'm doing something with a resource, like a file
. . . and something goes wrong
(This will raise a division by zero exception when I try to "print(2/0)")
. . . and I want to handle the exception gracefully:
Oops! Something happened! ( division by zero )
Traceback (most recent call last):
File "/home/pi/Desktop/test.py", line 9, in <module>
print(2/0)
ZeroDivisionError: division by zero
─────────────────────────────────────────────────────────────────────────────────────────────────
Python 3.7.3 (/usr/bin/python3)
>>>
“回溯”似乎已經存在,因為回溯在未處理的例外上是自動的
如果我使用 try/except 塊,為什么我必須匯入回溯,但如果我不使用則不要?
uj5u.com熱心網友回復:
回溯反映了 Python 解釋器在執行某些代碼期間在給定點的狀態。Python解釋器顯然可以訪問自己的狀態,并在拋出例外但未處理時通過列印該錯誤訊息與用戶共享它。它還允許代碼訪問它——通過 traceback 模塊,這就是你需要匯入它的原因。
uj5u.com熱心網友回復:
當引發未在 a 中捕獲的例外時,Python 的默認行為try...except是列印回溯并退出。如果你確實抓住了它,那么默認情況就不會發生。你必須決定如何處理它。如果您決定列印回溯,您現在必須通過匯入回溯模塊并使用其中的方法來顯式執行此操作。但是,這并不是絕對必要的,因為您可以檢查當前的回溯物件(來自sys.exc_info()),并撰寫自己的格式化程式。
uj5u.com熱心網友回復:
“回溯”似乎已經存在,因為回溯在未處理的例外上是自動的
您所看到的是 的默認行為sys.excepthook,即在程式退出未處理的例外之前將回溯列印到 stderr。
親自查看:
# example.py
import sys
sys.excepthook = lambda *args: None
print("before")
1/0
print("after")
上面的腳本將退出非零,但不會列印回溯,因為掛鉤已替換為無操作。并且“之后”不會被列印,因為該程序已經退出。因此,對于未處理的例外,回溯并不是“自動”的,而是默認配置。
通過呼叫sys.exit(0)你故意抑制ZeroDivisionError例外。解釋器將以零回傳碼退出,并且就 except 掛鉤而言,您已“處理”ZeroDivisionError并選擇退出回溯列印(src)。
uj5u.com熱心網友回復:
回溯存在,解釋器可以使用它(例如列印它),但只有匯入它才能使用它。如果你不使用 try/except 塊,你就沒有機會匯入它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/426245.html
上一篇:LdapConnectionSearchRequest拋出物件不存在錯誤-重訪
下一篇:ValueError沒有被捕獲
