This question is in reference to:
The "correct" way to define an exception in Python without PyLint complaining
In terms of clean code, looks like the ideal way to define an exception would be this:
class MyException(Exception):
pass
However as stated in the referenced question, this results in a runtime warning:
DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
The accepted answer seems to be to define the message method:
class MyException(Exception):
def __init__(self, message):
super(MyException, self).__init__(message)
self.message = message
Besides being 4 messier lines and less readable lines of code, the biggest issue with this is that you have to redundantly type the name of the new exception (MyException
) twice.
I don't use classes in Python very often, so forgive me if I'm way off base here, but might a better way to handle this be to define your own base exception with a non-depreciated .message
method since Python seems to have a problem with using the one from BaseException
? ::
# Do this only once:
class MyBaseException(Exception):
def __init__(self, message):
super(MyBaseException, self).__init__(message)
self.message = message
# Whenever you want to define a new exception:
class NewException(MyBaseException):
pass