I know that if I want to re-raise an exception, I simple use raise
without arguments in the respective except
block. But given a nested expression like
try:
something()
except SomeError as e:
try:
plan_B()
except AlsoFailsError:
raise e # I'd like to raise the SomeError as if plan_B()
# didn't raise the AlsoFailsError
how can I re-raise the SomeError
without breaking the stack trace? raise
alone would in this case re-raise the more recent AlsoFailsError
. Or how could I refactor my code to avoid this issue?
plan_B
in another function that returnsTrue
on success, andFalse
on exception? Then the outerexcept
block could just beif not try_plan_B(): raise
– Margaarg
and I'd try callingarg.plan_B()
which might raise anAttributeError
due toarg
not providing a plan B – Ibbetsonplan_B
to raise exceptions – Ibbetsonraise
w/o arguments. The code correcly reraises 1st exception. The comment is misleading: why reraise is inexcept AlsoFailsError
block if the comment tells "as if plan_B() didn't raise the AlsoFailsError". – Edemaraise from
now anyway – Ibbetson