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: 
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)

你可能感兴趣的:(踩坑)