Last active
December 31, 2023 20:59
-
-
Save jhazelwo/86124774833c6ab8f973323cb9c7e251 to your computer and use it in GitHub Desktop.
Supress traceback for custom Python exceptions
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
#!/usr/bin/env python3 | |
""" -*- coding: utf-8 -*- | |
Example Python3 Raise Exception without Traceback | |
Example Python3 Hide Traceback | |
Python3 Custom Exception Class | |
Python3 custom exception suppress traceback example | |
Python3 custom exception without traceback example | |
Example Python Raise Exception without Traceback | |
Example Python Hide Traceback | |
Python Custom Exception Class | |
Python custom exception suppress traceback example | |
Python custom exception without traceback example | |
""" | |
import sys | |
class QuietError(Exception): | |
# All who inherit me shall not traceback, but be spoken of cleanly | |
pass | |
class ParseError(QuietError): | |
# Failed to parse data | |
pass | |
class ArgumentError(QuietError): | |
# Some other problem with arguments | |
pass | |
class TooMany(QuietError): | |
# Too many results returned or values to unpack | |
pass | |
def quiet_hook(kind, message, traceback): | |
if QuietError in kind.__bases__: | |
print('{0}: {1}'.format(kind.__name__, message)) # Only print Error Type and Message | |
else: | |
sys.__excepthook__(kind, message, traceback) # Print Error Type, Message and Traceback | |
sys.excepthook = quiet_hook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. Neat trick