Skip to content

Instantly share code, notes, and snippets.

@denise-sanders
Last active July 9, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denise-sanders/45fb9a3af5a481dbbe0c6897d9889b7b to your computer and use it in GitHub Desktop.
Save denise-sanders/45fb9a3af5a481dbbe0c6897d9889b7b to your computer and use it in GitHub Desktop.
Python Exception Review
https://docs.python.org/2/library/exceptions.html#module-exceptions
Important ones (for Raising):
- TypeError
- ValueError
Declarign your own Exception if you dont need to override anything:
class Secret(Exception):
pass
Sending messages: raise MyException("My hovercraft is full of eels")
class ValidationError(Exception): # basically inherit from Exception. Or another one.
def __init__(self, message, errors):
# Call the base class constructor with the parameters it needs
super(ValidationError, self).__init__(message)
# Now for your custom code...
self.errors = errors
Diagnosing errors:
except Exception as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print(message)
@denise-sanders
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment