Last active
July 9, 2017 12:58
-
-
Save denise-sanders/45fb9a3af5a481dbbe0c6897d9889b7b to your computer and use it in GitHub Desktop.
Python Exception Review
This file contains 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All in one place: https://docs.python.org/2/library/exceptions.html#exception-hierarchy