Manually raising (throwing) an exception in Python

前端 未结 8 1138
长发绾君心
长发绾君心 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条回答
  •  日久生厌
    2020-11-22 04:28

    You should learn the raise statement of python for that. It should be kept inside the try block. Example -

    try:
        raise TypeError            #remove TypeError by any other error if you want
    except TypeError:
        print('TypeError raised')
    

提交回复
热议问题