如何在Python中捕获NotImplementedError异常?

如何在Python中捕获NotImplementedError异常?

用户定义的基类可以抛出NotImplementedError异常,以表明某个方法或行为需要由子类定义,模拟接口。此异常派生自RuntimeError。在用户定义的基类中,抽象方法应在需要派生类重写方法时抛出此异常。

示例

import sys
try:
    class Super(object):
        @property
        def example(self):
            raise NotImplementedError("子类应该实现这个函数!")
    s = Super()
    print s.example
except Exception as e:
    print e
    print sys.exc_type
Python

输出

子类应该实现这个函数!
<type 'exceptions.NotImplementedError'>
Python

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程