Skip to content

Instantly share code, notes, and snippets.

@svetlyak40wt
Created April 5, 2016 13:52
Show Gist options
  • Save svetlyak40wt/c8ed5134a4073d4f2780f018d2089de5 to your computer and use it in GitHub Desktop.
Save svetlyak40wt/c8ed5134a4073d4f2780f018d2089de5 to your computer and use it in GitHub Desktop.
This is a demo of broken traceback when reraising exception in python 2.x.
"""
This is a demo of broken traceback
when reraising exception in python 2.x.
In this code, "raise e" makes python
lost original traceback, attached to the
exception. It outputs:
Traceback (most recent call last):
File "/tmp/test.py", line 19, in <module>
another()
File "/tmp/test.py", line 14, in another
raise e
RuntimeError: blah
But if you'll fix this code and leave only
"raise" alone, then it will show original
traceback:
Traceback (most recent call last):
File "/tmp/test.py", line 19, in <module>
another()
File "/tmp/test.py", line 12, in another
some()
File "/tmp/test.py", line 8, in some
return bar()
File "/tmp/test.py", line 5, in bar
return foo()
File "/tmp/test.py", line 2, in foo
raise RuntimeError("blah")
RuntimeError: blah
"""
def foo():
raise RuntimeError("blah")
def bar():
return foo()
def some():
return bar()
def another():
try:
some()
except Exception as e:
raise e
if __name__ == '__main__':
another()
@idlesign
Copy link

idlesign commented Apr 8, 2016

Давно Бикинг писал статью о вариантах обхода такой ситуации. Одной из идей была, что, ежели e никак не дополняется, то голый raise должен справиться с задачей.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment