As of Python 3 the traceback is stored in the exception, so a simple raise e
will do the (mostly) right thing:
try:
something()
except SomeError as e:
try:
plan_B()
except AlsoFailsError:
raise e # or raise e from None - see below
The traceback produced will include an additional notice that SomeError
occurred while handling AlsoFailsError
(because of raise e
being inside except AlsoFailsError
). This is misleading because what actually happened is the other way around - we encountered AlsoFailsError
, and handled it, while trying to recover from SomeError
. To obtain a traceback that doesn't include AlsoFailsError
, replace raise e
with raise e from None
.
In Python 2 you'd store the exception type, value, and traceback in local variables and use the three-argument form of raise
:
try:
something()
except SomeError:
t, v, tb = sys.exc_info()
try:
plan_B()
except AlsoFailsError:
raise t, v, tb
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…