-
-
Save mdavis199/3f7f6605fb9e9a6d6b4b594b2d9796cc 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