有什麼不對下面的代碼(Python的下2.7.1):錯誤異常,必須從即使它BaseException(Python 2.7版)獲得
class TestFailed(BaseException):
def __new__(self, m):
self.message = m
def __str__(self):
return self.message
try:
raise TestFailed('Oops')
except TestFailed as x:
print x
當我運行它,我得到:
Traceback (most recent call last):
File "x.py", line 9, in <module>
raise TestFailed('Oops')
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
但在我看來,TestFailed
確實來自BaseException
。
對於誰也弄不清爲什麼他們收到此錯誤的其他人:檢查,以確保你不小心做'高清MyException(例外):pass',而不是所需要的'類MyException(例外):通過'。錯過錯誤很容易。 –