Skip to content

Instantly share code, notes, and snippets.

@mattbennett
Created August 3, 2015 10:40
Show Gist options
  • Save mattbennett/d42030daecb0ee773dce to your computer and use it in GitHub Desktop.
Save mattbennett/d42030daecb0ee773dce to your computer and use it in GitHub Desktop.
Raising vs reraising
import sys
def foo():
bar()
def bar():
baz()
def baz():
raise Exception("whoops")
def raises():
try:
foo()
except Exception:
raise
def reraises():
try:
foo()
except Exception as exc:
raise exc
if sys.argv[1] == "raise":
raises()
elif sys.argv[1] == "reraise":
reraises()
@mattbennett
Copy link
Author

$ python raising.py raise
Traceback (most recent call last):
  File "raising.py", line 31, in <module>
    raises()
  File "raising.py", line 18, in raises
    foo()
  File "raising.py", line 5, in foo
    bar()
  File "raising.py", line 9, in bar
    baz()
  File "raising.py", line 13, in baz
    raise Exception("whoops")
Exception: whoops

vs

$ python raising.py reraise
Traceback (most recent call last):
  File "raising.py", line 33, in <module>
    reraises()
  File "raising.py", line 27, in reraises
    raise exc
Exception: whoops

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