I get a warning that BaseException.message is deprecated in Python 2.6 when I use the following user-defined exception:
class MyException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
This is the warning:
DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
self.message = message
What's wrong with this? What do I have to change to get rid of the deprecation warning?
pzrq's post says to use:
This was exactly what I needed.
(If you are in a unicode environment, it appears that:
will work, and it appears to work fine in a non-unicode environment)
Pzrq said a lot of other good stuff, but I almost missed their answer due to all the good stuff. Since I don't have 50 points I cannot comment on their answer to attempt to draw attention to the simple solution that works, and since I don't have 15 I cannot vote that answer up, but I can post (feels backward, but oh well) - so here I am posting - probably lose points for that...
Since my point is to draw attention to pzrq's answer, please don't glaze over and miss it in all the below. the first few lines of this post are the most important.
My story:
The problem I came here for was if you want to catch an exception from a class that you have no control over - what then??? I'm certainly not going to subclass all possible classes my code uses in an attempt to be able to get a message out of all possible exceptions!
I was using:
which, as we all now know, gives the warning OP asked about (which brought me here), and this, which pzrq gives as a way to do it:
did not.
I'm not in a unicode environment, but jjc's answer made me wonder, so I had to try it. In this context this becomes:
which, to my surprise, worked exactly like str(e) - so now that's what I'm using.
Don't know if 'str(e)/unicode(e)' is the 'approved Python way', and I'll probably find out why that's not good when I get to 3.0, but one hopes that the ability to handle an unexpected exception (*) without dying and still get some information from it won't ever go away...
(*) Hmm. "unexpected exception" - I think I just stuttered!
How to replicate the warning
Let me clarify the problem, as one cannot replicate this with the question's sample code, this will replicate the warning in Python 2.6 and 2.7, if you have warnings turned on (via the
-W
flag, thePYTHONWARNINGS
environment variable, or the warnings module):Stop using
.message
I prefer
repr(error)
, which returns a string that contains the name of the error type, the repr of the message, if there is one, and the repr of the remaining arguments.Eliminating the warning while still using
.message
And the way you get rid of the
DeprecationWarning
is to subclass a builtin exception as the Python designers intended:getting just the
.message
attribute withouterror.message
If you know there was one argument, a message, to the Exception and that's what you want, it is preferable to avoid the message attribute and just take the
str
of the error. Say for a subclassedException
:And usage:
See also this answer:
Proper way to declare custom exceptions in modern Python?