+3  A: 

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.

M. Utku ALTINKAYA
+12  A: 

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

gahooa
+1. It's interesting to know that the arguments passed to the constructor can be retrieved in the `args` attribute (it's a tuple).
Bastien Léonard
Hmm, that does seem to work nicely and satisfies my desire not to type much. The default str() and repr() methods in Exception seem to do a good job of printing out any arguments passed into the MyException() constructor. Is this using Exception.args? Is it good style in modern Python? If I need to override \_\_init\_\_ for some reason, what's the right way to fill *args?
Nelson
+1. The OP doesn't need to do anything tricky, so why write boilerplate to do what the base `Exception` class already does?
Jeff Bradberry
@Nelson: see the Edit above for a good way to add behavior.
gahooa
ty for help. For future posterity: PEP 0352's sample code for BaseException shows exactly what's going on with args, \_\_str()\_\_, etc.
Nelson
+1  A: 

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. ;)

Lennart Regebro