Python 3 有整洁的
try:
raise OneException('sorry')
except OneException as e:
# after a failed attempt of mitigation:
raise AnotherException('I give up') from e
允许在不丢失上下文的情况下引发后续异常的语法。在 Python 2 中我能想到的最好的类比是
raise AnotherException((e,'I give up')), None, sys.exc_info()[2]
(e,'')
将原始异常的名称包含在消息中是一个丑陋的技巧。但是没有更好的方法吗?