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

前端 未结 12 1138
情书的邮戳
情书的邮戳 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

    try:
        try:
            int('a')
        except ValueError as e:
            raise ValueError('There is a problem: {0}'.format(e))
    except ValueError as err:
        print err
    

    prints:

    There is a problem: invalid literal for int() with base 10: 'a'
    

提交回复
热议问题