You can store the exception type, value, and tracker in local variables and use the raise three-argument of the form :
try: something() except SomeError: t, v, tb = sys.exc_info() try: plan_B() except AlsoFailsError: raise t, v, tb
In Python 3, tracing is saved in an exception, so raise e will do the (mostly) right thing:
try: something() except SomeError as e: try: plan_B() except AlsoFailsError: raise e
The only problem with the above is that it will produce a slightly misleading tracker that tells you that SomeError occurred during the processing of AlsoFailsError (due to raise e inside, except AlsoFailsError ), where the exact opposite actually happened - we pumped AlsoFailsError trying to recover from SomeError . To disable this behavior and get AlsoFailsError , which AlsoFailsError is never mentioned AlsoFailsError , replace raise e with raise e from None .
user4815162342 Aug 12 '13 at 13:47 on 2013-08-12 13:47
source share