Last active
June 11, 2021 14:20
-
-
Save matthewpoer/cb71464019e4f1f341bc4361e17eecd7 to your computer and use it in GitHub Desktop.
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
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) |
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
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