How to imitate Python 3's raise ... from in Python 2?
Asked Answered
S

2

32

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?

Shondrashone answered 5/12, 2014 at 14:41 Comment(4)
You could do just raise without any argument, but I don't think you could change the type to AnotherException if you did that.Amann
@Amann I know, this is just a simplification - in a more realistic case this would be something like a file not found exception and its failsafe also failing, thus yielding a different exception with some other message as well, but the original exception should be conservedShondrashone
I don't think Python 2 provides proper support for anything like this. I suppose you could wrap your ugly hack in a reraise() 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
@Amann hg.python.org/peps/rev/… :P Currently the major reason I use Python 2.7 is PythonXY (Windows), but I'm also asking out of curiosityShondrashone
W
31

There's a raise_from in python-future; simply install it

pip install future

and import to use

from future.utils import raise_from
# or: from six import reraise as raise_from

class FileDatabase:
    def __init__(self, filename):
        try:
            self.file = open(filename)
        except IOError as exc:
            raise_from(DatabaseError('failed to open'), exc)

UPDATE

The compatibility package six also supports raise_from, from version 1.9 (released in 2015). It is used in the same manner as above.

Whitcher answered 23/4, 2015 at 20:46 Comment(2)
Hi. raise_from in six actually imitates plain raise(exec) in python2, so it doesn't get python 3 behavior :(Brahms
Yes, raise_from in six imitates plain raise. But reraise, also from six, worked. See the example in this link: python-future.org/compatible_idioms.html or in my suggested solution.Verbenia
V
8

Instead of using six.raise_from, try to use six.reraise, as explained in this page:

http://python-future.org/compatible_idioms.html

from six import reraise as raise_ 
# or from future.utils import raise_

traceback = sys.exc_info()[2]
raise_(ValueError, "dodgy value", traceback)
Verbenia answered 20/3, 2017 at 13:40 Comment(1)
six.reraise sets the trace, not __cause__. This question is asking about explicit chaining, which sets the latter.Touzle

© 2022 - 2024 — McMap. All rights reserved.