Skip to content

Instantly share code, notes, and snippets.

@matthewpoer
Last active June 11, 2021 14:20
Show Gist options
  • Save matthewpoer/cb71464019e4f1f341bc4361e17eecd7 to your computer and use it in GitHub Desktop.
Save matthewpoer/cb71464019e4f1f341bc4361e17eecd7 to your computer and use it in GitHub Desktop.
class CustomException(Exception):
def __init__(self, extra_context, message):
self.extra_context = extra_context
self.message = message
super().__init__(self.message)
def __str__(self):
return f'Shit going down! {self.message} | extra context: {self.extra_context}'
try:
raise CustomException("extra context", "some message")
except (CustomException, Exception) as exception:
print(exception)
try:
raise Exception("Some generic exception")
except (CustomException, Exception) as exception:
print(exception)
Python 3.9.5 (default, May 4 2021, 03:36:27)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class CustomException(Exception):
... def __init__(self, extra_context, message):
... self.extra_context = extra_context
... self.message = message
... super().__init__(self.message)
... def __str__(self):
... return f'Shit going down! {self.message} | extra context: {self.extra_context}'
...
>>> try:
... raise CustomException("extra context", "some message")
... except (CustomException, Exception) as exception:
... print(exception)
...
Shit going down! some message | extra context: extra context
>>> try:
... raise Exception("Some generic exception")
... except (CustomException, Exception) as exception:
... print(exception)
...
Some generic exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment