如何在 Python 中捕获 NotImplementedError 异常?
pythonserver side programmingprogramming
用户定义的基类可以引发 NotImplementedError 来指示需要由子类定义方法或行为,模拟接口。此异常源自 RuntimeError。在用户定义的基类中,抽象方法在需要派生类重写该方法时应引发此异常。
示例
import sys try: class Super(object): @property def example(self): raise NotImplementedError("Subclasses should implement this!") s = Super() print s.example except Exception as e: print e print sys.exc_type
输出
Subclasses should implement this! <type 'exceptions.NotImplementedError'>