Python 3很整洁
try:
raise OneException('sorry')
except OneException as e:
# after a failed attempt of mitigation:
raise AnotherException('I give up') from e
Run Code Online (Sandbox Code Playgroud)
允许在不丢失上下文的情况下引发后续异常的语法.我在Python 2中可以提出的最好的比喻是
raise AnotherException((e,'I give up')), None, sys.exc_info()[2]
Run Code Online (Sandbox Code Playgroud)
这(e,'')是一个丑陋的黑客,它将原始异常的名称包含在消息中.但是不是有更好的方法吗?