2

So I don't know if this is good practice, it probably is not but...

I have a series of custom errors/warnings under a general error class which hands general stuff for them.

class Error(Exception):
    """Main error class."""

    def __init__(self, *args):
        """Passable args."""
        self.args = args
        return None

class ValueOverwrite(Error):
    """When a value in a dictionary is overwriten."""
    pass

class SaveOverwrite(Error):
    """When a file is overwriten"""
    pass

When you raise an error and catch it could can do what you want with it but what if you want to simplify it so you only need to raise it - changing what is executed/printed.

When something is raised and not caught it prints something like this

Traceback (most recent call last):
  File ".\errortest.py", line 20, in <module>
    raise Error("my msg", "a", "v")
__main__.Error: ('my msg', 'a', 'v')

I want to change what is printed here for these errors. Or at least the bit after __main__.Error: (I worked this out) To do this my guess is I would need to overwrite one or more of the methods in the exception or baseexception classes but I don't know as looking around many sites, articles, python doc I can not find anything on it. The source code is here but it is in C, I kind of get some of it but I don't know what it all does or what the called functions are. There is literally no documentation on the code in them anywhere just things like built-in errors and how to make custom ones.

I now don't think this is a good way to do it as even if you print something different it will still break following the code?

How should I do it? or where are the modules with these classes in? Or just stick to something like this:

try:
    raise Error("my msg", "a", "v")
except Error as e:
    print(e.args[0])
Max
  • 145
  • 2
  • 15
  • 1
    So you want to customize the global [`excepthook`](https://docs.python.org/2/library/sys.html#sys.excepthook)? – dhke Mar 18 '17 at 22:19
  • Possible duplicate of [Python Global Exception Handling](http://stackoverflow.com/questions/6598053/python-global-exception-handling) – dhke Mar 18 '17 at 22:20
  • @dhke from looking at excepthook, i think that is what i want to customize. Looking at the other question you linked i think this answer is [one](http://stackoverflow.com/a/6598286/5990054) way i could do it but is there a way i can edit from within the Error class to make it cleaner? (so i could not have to check the error type to the ones i wanted) _also if it matters python 3_ – Max Mar 18 '17 at 22:34
  • I looked through the [source file](https://hg.python.org/cpython/file/3.6/Python/sysmodule.c) excepthook and what is is called by (stderr) are in but i don't know what all the called functions do but that does not really matter. It does not help through, i don't think. To really do it in the error class I need to find the Exception class or the base one... – Max Mar 18 '17 at 23:20
  • @dhke i found it https://github.com/python/cpython/blob/master/Objects/exceptions.c but it gets me no where really as i don't know C and all the calls to functions. `super(Error, self).__init__(msg)` can be used to print only certain args however if there is * in the function args it mucks it up back to before. I still think there would be a problem even if we changed the text printed as if I do not catch the error with except, it will stop the code right? or could that be overwritten too? – Max Mar 19 '17 at 00:39

0 Answers0