Manually raising (throwing) an exception in Python

前端 未结 8 1133
长发绾君心
长发绾君心 2020-11-22 03:38

How can I raise an exception in Python so that it can later be caught via an except block?

8条回答
  •  萌比男神i
    2020-11-22 04:08

    DON'T DO THIS. Raising a bare Exception is absolutely not the right thing to do; see Aaron Hall's excellent answer instead.

    Can't get much more pythonic than this:

    raise Exception("I know python!")
    

    See the raise statement docs for python if you'd like more info.

提交回复
热议问题