I would like to declare a hierarchy of user-defined exceptions in Python. However, I would like my top-level user-defined class (TransactionException
) to be abstract. That is, I intend TransactionException
to specify methods that its subclasses are required to define. However, TransactionException
should never be instantiated or raised.
I have the following code:
from abc import ABCMeta, abstractmethod
class TransactionException(Exception):
__metaclass__ = ABCMeta
@abstractmethod
def displayErrorMessage(self):
pass
However, the above code allows me to instantiate TransactionException
...
a = TransactionException()
In this case a
is meaningless, and should instead draw an exception. The following code removes the fact that TransactionException
is a subclass of Exception
...
from abc import ABCMeta, abstractmethod
class TransactionException():
__metaclass__ = ABCMeta
@abstractmethod
def displayErrorMessage(self):
pass
This code properly prohibits instantiation but now I cannot raise a subclass of TransactionException
because it's not an Exception
any longer.
Can one define an abstract exception in Python? If so, how? If not, why not?
NOTE: I'm using Python 2.7, but will happily accept an answer for Python 2.x or Python 3.x.