Raise two errors at the same time

后端 未结 6 1233
一个人的身影
一个人的身影 2021-02-19 03:10

Is there any way to raise two errors at the same time by using try and except? For example, ValueError and KeyError.

How do I do that?

6条回答
  •  粉色の甜心
    2021-02-19 03:41

    You could raise an error which inherits from both ValueError and KeyError. It would get caught by a catch block for either.

    class MyError(ValueError, KeyError):
        ...
    

提交回复
热议问题