You should override __repr__
or __unicode__
methods instead of using message, the args you provide when you construct the exception will be in args
member variable when you need.
Maybe I missed the question, but why not:
class MyException(Exception):
pass
Edit: to override somethign (or pass extra args), do this:
class ValidationError(Exception):
def __init__(self, message, Errors):
# Call the base class constructor with the parameters it needs
Exception.__init__(self, message)
# Now for your custom code...
self.Errors = Errors
That way you could pass dict of error messages to the second param, and get to it later with e.Errors
No, "message" is not forbidden. It's just deprecated. You application will work fine with using message. But you may want to get rid of the deprecation error, of course.
When you create custom Exception classes for your application, many of them do not subclass just from Exception, but from others, like ValueError or similar. Then you have to adapt to their usage of variables.
And if you have many exceptions in your application it's usually a good idea to have a common custom base class for all of them, so that users of your modules can do
try:
...
except NelsonsExceptions:
...
And in that case you can do the __init__ and __str__
needed there, so you don't have to repeat it for every exception. But simply calling the message variable something else than message does the trick.
In any case, you only need the __init__ or __str__
if you do something different from what Exception itself does. And because if the deprecation, you then need both, or you get an error. That's not a whole lot of extra code you need per class. ;)