如何在 Python 中捕捉 NotImplementedError 异常?
用户定义基类可以通过引发 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'>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
JavaScript
PHP