How to pass a variable to an exception when raised and retrieve it when excepted?

前端 未结 2 882
太阳男子
太阳男子 2021-02-03 19:25

Right now I just have a blank exception class. I was wondering how I can give it a variable when it gets raised and then retrieve that variable when I handle it in the try...exc

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-03 19:52

    You can do this.

    try:
        ex = ExampleException()
        ex.my_variable= "some value"
        raise ex
    except ExampleException, e:
        print( e.my_variable )
    

    Works fine.

提交回复
热议问题