如何在 Python 中捕获 NotImplementedError 异常?
pythonserver side programmingprogramming更新于 2025/6/20 9:16:17
用户定义的基类可以引发 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'>
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

