How to imitate Python 3's raise … from in Pyth

2019-04-03 10:50发布

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?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-04-03 10:57

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)
查看更多
一夜七次
3楼-- · 2019-04-03 11:05

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 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.

查看更多
登录 后发表回答