Skip to content

Instantly share code, notes, and snippets.

@thigm85
Created March 8, 2016 14:05
Show Gist options
  • Save thigm85/f6147186210961e3e607 to your computer and use it in GitHub Desktop.
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
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