Created
March 8, 2016 14:05
-
-
Save thigm85/f6147186210961e3e607 to your computer and use it in GitHub Desktop.
Example where an exception is raised with variables that can be accessed by the exception handler. Reference: https://docs.python.org/2/tutorial/errors.html#handling-exceptions
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
| try: | |
| raise Exception('spam', 'eggs') | |
| except Exception as inst: | |
| print type(inst) # the exception instance | |
| print inst.args # arguments stored in .args | |
| print inst # __str__ allows args to be printed directly | |
| x, y = inst.args | |
| print 'x =', x | |
| print 'y =', y | |
| # output: | |
| # <type 'exceptions.Exception'> | |
| # ('spam', 'eggs') | |
| # ('spam', 'eggs') | |
| # x = spam | |
| # y = eggs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment