TypeError: catching classes that do not inherit from BaseException is not allowed

写了一个demo异常类,没有继承BaseException类,提示报如下错误,很明显只要继承一下BaseException就可以了。
TypeError: catching classes that do not inherit from BaseException is not allowed 捕获到一个没有继承BaseException的异常类(这是不被允许的)。

Exception ignored in: <generator object demo_exc_handling at 0x000001D06C66E200>
Traceback (most recent call last):
  File "a5_3_coroutine_exception.py", line 10, in demo_exc_handling
TypeError: catching classes that do not inherit from BaseException is not allowed
class DemoException():  # DemoException(BaseException) 为正确写法
    """demo异常类型"""
    pass

def demo_exc_handling():
    print('-> coroutine started')
    while True:
        try:
            var = yield
        except DemoException as e:
            print('*** DemoException handled. Continuing...')
        else:
            print('-> coroutine received: {!r}'.format(var))
    # raise RuntimeError('This line should never run.')

cor_exc = demo_exc_handling()
cor_exc.send(None)
cor_exc.send(1)
发布了87 篇原创文章 · 获赞 43 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_31362767/article/details/104764571