我使用的是其中在一個點一個異常被定義爲一個Python庫:如何捕捉自定義異常在Python
raise Exception("Key empty")
我現在希望能夠趕上那特定的異常,但我我不知道該怎麼做。
我嘗試以下
try:
raise Exception('Key empty')
except Exception('Key empty'):
print 'caught the specific exception'
except Exception:
print 'caught the general exception'
但只是打印出caught the general exception
。
有沒有人知道我怎麼可以捕捉到特定的Key empty
異常?歡迎所有提示!
但LIB是不是我的控制之下。它是開源的,所以我可以編輯它,但我最好試着在不編輯庫的情況下捕捉它。這不可能嗎? – kramer65
我甚至會選擇RuntimeError作爲基類。 – Gribouillis
如果庫引發的異常是固定的。然後你必須捕捉到這個例外。您可以捕獲該異常並提出您自己的異常作爲回報。 –