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

Tobias Kienzler

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?

tutuDajuju

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Python "raise from" usage

From Java

How to use "raise" keyword in Python

From Dev

Python 'string' % [1, 2, 3] doesn't raise TypeError

From Dev

Python 3 raise TypeError, 'Time.milliseconds expects a datetime object

From Dev

Python/SQLite3 raise function error

From Dev

Python2 "raise e[0], e[1], e[2]" and python3 version in one file

From Dev

How to raise an exception in Python 3.3

From Dev

How to use .so modules from Python 2 in Python 3?

From Dev

Scope for "raise" without arguments in nested exception handlers in Python 2 and 3

From Dev

Python raise NotADirectoryError

From Dev

Unexpectable "except" raise in Python

From Dev

How do I raise DeprecationWarnings in Python 2.7?

From Dev

How do I set a Python builtin Exception's message attribute when I raise it

From Dev

Why is Python 3's PrettyPrinter behaving differently from Python 2's, and how do I get the same behavior?

From Dev

how to run pip for python 3 and python 2

From Dev

How to list all exceptions a function could raise in Python 3?

From Dev

How to raise Python exception from a C extension

From Dev

Python 2 and 3 from Powershell

From Dev

How to convert sort using cmp from python 2 to python 3?

From Dev

How to change jupyter kernel from Python 2 to python 3?

From Dev

How to raise an exception in Python 3.3

From Dev

Unexpectable "except" raise in Python

From Dev

How do I set a Python builtin Exception's message attribute when I raise it

From Dev

raise exception wave python

From Dev

Python decorator that let's method run or raise an exception

From Dev

How to port `__slots__` from Python 2 to 3

From Dev

How to use python to imitate the opening and saving of an image file in an image editor?

From Dev

Combine multiple try except with raise - Sign up users - Python 3

From Dev

plotting 3D surface using python: raise ValueError("Argument Z must be 2-dimensional.") matplotlib

Related Related

  1. 1

    Python "raise from" usage

  2. 2

    How to use "raise" keyword in Python

  3. 3

    Python 'string' % [1, 2, 3] doesn't raise TypeError

  4. 4

    Python 3 raise TypeError, 'Time.milliseconds expects a datetime object

  5. 5

    Python/SQLite3 raise function error

  6. 6

    Python2 "raise e[0], e[1], e[2]" and python3 version in one file

  7. 7

    How to raise an exception in Python 3.3

  8. 8

    How to use .so modules from Python 2 in Python 3?

  9. 9

    Scope for "raise" without arguments in nested exception handlers in Python 2 and 3

  10. 10

    Python raise NotADirectoryError

  11. 11

    Unexpectable "except" raise in Python

  12. 12

    How do I raise DeprecationWarnings in Python 2.7?

  13. 13

    How do I set a Python builtin Exception's message attribute when I raise it

  14. 14

    Why is Python 3's PrettyPrinter behaving differently from Python 2's, and how do I get the same behavior?

  15. 15

    how to run pip for python 3 and python 2

  16. 16

    How to list all exceptions a function could raise in Python 3?

  17. 17

    How to raise Python exception from a C extension

  18. 18

    Python 2 and 3 from Powershell

  19. 19

    How to convert sort using cmp from python 2 to python 3?

  20. 20

    How to change jupyter kernel from Python 2 to python 3?

  21. 21

    How to raise an exception in Python 3.3

  22. 22

    Unexpectable "except" raise in Python

  23. 23

    How do I set a Python builtin Exception's message attribute when I raise it

  24. 24

    raise exception wave python

  25. 25

    Python decorator that let's method run or raise an exception

  26. 26

    How to port `__slots__` from Python 2 to 3

  27. 27

    How to use python to imitate the opening and saving of an image file in an image editor?

  28. 28

    Combine multiple try except with raise - Sign up users - Python 3

  29. 29

    plotting 3D surface using python: raise ValueError("Argument Z must be 2-dimensional.") matplotlib

HotTag

Archive