如何在 Python 中捕获 NotImplementedError 异常?
用户定义基类可抛出 NotImplementedError,以表示 subclass 需要定义方法或行为,模拟一个接口。该异常来自 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'>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP