我只是想確保我以正確的「pythonic」方式做到這一點 - 我想確保我已經定義了 - 並且正在使用這個異常類。特別是eval(repr())邏輯 - 主要是爲了清潔,我明白你爲什麼最終引用了字符串repr()返回的引號,但我不喜歡記錄它們。這是一種定義和使用Python異常的適當方式嗎?
class IPCClientError(Exception):
""" General IPC Client Exception class """
def __init__(self, value = "Unspecified error"):
self.val = value + ", see IPC client log for details."
def __str__(self):
return eval(repr(self.val))
當我拋出異常,我使用類似:
raise IPCClientError("Socket error")
然後調用方法是這樣的:
except IPCClientError, exc:
self.log.error(str(exc))
return ERROR
是的你做得對,除非你應該重新提出異常。 –
你說得對,我應該,也許下一次我有機會做一些重構:) – Dave
爲什麼你使用'return eval(repr(self.val))'而不是'return self.val'? – jamessan