Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created June 14, 2019 03:48
Show Gist options
  • Select an option

  • Save allenyang79/8b963bf5f28de451f234bc1ef1e5b483 to your computer and use it in GitHub Desktop.

Select an option

Save allenyang79/8b963bf5f28de451f234bc1ef1e5b483 to your computer and use it in GitHub Desktop.
python 2.7 re-raise
exc_type, exc_val, tb = sys.exc_info()
raise CustomError("oops," +str(e), ["AAA", "BBB"]), None, tb
# raise error
import traceback
import sys
class CustomError(Exception):
pass
def reraise(e, new_type=None, new_value=None):
"""
Args:
e Exception: orig exception instance
new_type: new Exception class
new_value: args for new error type, value for reraise. if none . raise original error message
code-block:
try:
1/0
except Exception as e:
reraise(e, CustomError, "oops, " + str(e))
"""
exc_type, exc_val, tb = sys.exc_info()
exc_type = new_type or exc_type
exc_val = new_value or exc_val
raise exc_type, exc_val, tb
try:
try:
1/0
except Exception as e:
reraise(e, CustomError, "oops, " + str(e))
except Exception as e:
print(repr(e))
print("=========")
traceback.print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment