How do I raise the same Exception with a custom message in Python?

前端 未结 12 1120
情书的邮戳
情书的邮戳 2020-12-12 09:45

I have this try block in my code:

try:
    do_something_that_might_raise_an_exception()
except ValueError as err:
    errmsg = \'My custom error         


        
12条回答
  •  轮回少年
    2020-12-12 10:01

    Python 3 built-in exceptions have the strerror field:

    except ValueError as err:
      err.strerror = "New error message"
      raise err
    

提交回复
热议问题