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])