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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章