python异常处理模块raise NotImplementedError

raise NotImplementedError的使用感觉很类似于C#中虚函数的效果,它的意思是如果这个方法没有被子类重写,但是调用了,就会报错。

唯一的作用就是把这个异常内置进去,然后写父类方法的时候抛出这个异常,报错的时候比较容易看,最终为了方便调试bug。


'''


# class TestClassOne(object):
#
#     def Test(self):
#         raise NotImplementedError
#
# class TestClassTwo(TestClassOne):
#
#     def Test2(self):
#         print('hello world')
#
# a = TestClassTwo()
# a.Test()


# coding=utf-8
# class TestClassOne(object):
#     def Test(self):
#         raise NotImplementedError
#
# class TestClassThree(TestClassOne):
#     def Test(self):
#         print('hello world')
#
# b = TestClassThree()
# b.Test()
#
# import abc
#
#
# class MyAbstractClass(object):
#
#     @abc.abstractmethod
#     def my_method(self):
#         print('hello world')
#         pass
# T = MyAbstractClass()
# T.my_method()

class ClassDemo:
    def test_demo(self):
        raise NotImplementedError("test:代码从这里出现异常啦!请尽快修复!")


class ChildClass(ClassDemo):
    pass

inst = ChildClass()
inst.test_demo()

输出结果:

Traceback (most recent call last):
  File "ors.py", line 56, in <module>
    inst.test_demo()
  File "ors.py", line 49, in test_demo
    raise NotImplementedError("test:代码从这里出现异常啦!请尽快修复!")
NotImplementedError: test:代码从这里出现异常啦!请尽快修复!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值