我在我的代码中有这个try块:

try:
    do_something_that_might_raise_an_exception()
except ValueError as err:
    errmsg = 'My custom error message.'
    raise ValueError(errmsg)

严格地说,我实际上引发了另一个ValueError,而不是do_something…()抛出的ValueError,在这种情况下被称为err。如何将自定义消息附加到错误?我尝试以下代码,但失败,由于错误,ValueError实例,不可调用:

try:
    do_something_that_might_raise_an_exception()
except ValueError as err:
    errmsg = 'My custom error message.'
    raise err(errmsg)

当前回答

我尝试了这个精简版的@RobinL,效果很好:

try:
    do_something_that_might_raise_an_exception()
except ValueError as e:
    raise ValueError(f'Custom text {e}')

其他回答

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

打印:

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

如果你足够幸运只支持python 3。X,这真的变成了一件美丽的事情:)

提高从

我们可以使用raise from链接异常。

try:
    1 / 0
except ZeroDivisionError as e:
    raise Exception('Smelly socks') from e

在这种情况下,调用者将捕获的异常具有引发异常的位置的行号。

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    1 / 0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    raise Exception('Smelly socks') from e
Exception: Smelly socks

注意,底部异常只有我们引发异常的堆栈跟踪。调用者仍然可以通过访问他们捕获的异常的__cause__属性来获得原始异常。

with_traceback

或者你可以使用with_traceback。

try:
    1 / 0
except ZeroDivisionError as e:
    raise Exception('Smelly socks').with_traceback(e.__traceback__)

使用这种形式,调用者将捕获的异常具有原始错误发生位置的回溯。

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    1 / 0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    raise Exception('Smelly socks').with_traceback(e.__traceback__)
  File "test.py", line 2, in <module>
    1 / 0
Exception: Smelly socks

注意,底部的异常中有执行无效除法的行,也有重新引发异常的行。

引发相同的错误,并在前面添加自定义文本消息。 (编辑-抱歉,实际上和https://stackoverflow.com/a/65494175/15229310一样,为什么有10个(好评)“解决方案”只是不回答张贴的问题?)

    try:
       <code causing exception>
    except Exception as e:
        e.args = (f"My custom text. Original Exception text: {'-'.join(e.args)}",)
        raise

当前的答案对我来说并不是很好,如果没有重新捕获异常,则不会显示附加的消息。

但是像下面这样做既保持跟踪,又显示附加的消息,不管是否重新捕获异常。

try:
  raise ValueError("Original message")
except ValueError as err:
  t, v, tb = sys.exc_info()
  raise t, ValueError(err.message + " Appended Info"), tb

(我使用Python 2.7,在Python 3中没有尝试过)

Python 3内置异常有strerror字段:

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