Created
November 21, 2016 15:47
-
-
Save chendi0x7C00/a79f6b80bfdfa98759e5cfb800e26d82 to your computer and use it in GitHub Desktop.
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
| # coding=UTF-8 | |
| def lost_exception(): | |
| while True: | |
| try: | |
| print("hello world") | |
| raise IndexError("rrr") | |
| except NameError as e: | |
| print 'NameError happened' | |
| print e | |
| finally: | |
| # IndexError is saved for reraise | |
| # but it won't reraise if | |
| # 1. new excepton happens in finally | |
| # 2. return | |
| # 3. break | |
| print("finally executed") | |
| break | |
| def false_return(n): | |
| try: | |
| if n <= 0: | |
| raise ValueError("n must > 0") | |
| else: | |
| # return in finally gets executed first | |
| return n | |
| except ValueError as e: | |
| print("ValueError") | |
| print e | |
| finally: | |
| print("in finally") | |
| return -1 | |
| def main(): | |
| # lost_exception() | |
| print(false_return(-100)) | |
| print(false_return(10)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment