Created
June 14, 2019 03:48
-
-
Save allenyang79/8b963bf5f28de451f234bc1ef1e5b483 to your computer and use it in GitHub Desktop.
python 2.7 re-raise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| exc_type, exc_val, tb = sys.exc_info() | |
| raise CustomError("oops," +str(e), ["AAA", "BBB"]), None, tb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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