Python 3 has the neat
try:
raise OneException('sorry')
except OneException as e:
# after a failed attempt of mitigation:
raise AnotherException('I give up') from e
syntax which allows raising a followup exception without loosing context. The best analogy I could come up with in Python 2 is
raise AnotherException((e,'I give up')), None, sys.exc_info()[2]
where the (e,'')
is an ugly hack to have the original exception's name included in the message. But isn't there a better way?
raise
without any argument, but I don't think you could change the type toAnotherException
if you did that. – Amannreraise()
function or something. Given the release schedule for 2.8, you might consider switching to 3.x. Do you have any 2.x-only dependencies? – Amann