As I am sure you are well aware, python, as do most programming languages, comes with built-in exceptions. I have gone through the list, and cannot deduce which would be appropriate. Of course I can make my own exception, but this is a fairly standard error that would be excepted.
This is an error based on instance relations. Instances of a class are related to only some of the other instances. Computations can be made depending on the different connections. This error will be raise
d if a computation is attempted on an unrelated instance.
example
class Foo:
def __init__(self,related=None):
'''related is a list of Foo instances'''
if related is not None:
self.related = related
else:
self.related = []
def compute(self,instance):
if instance in self.related:
#execute code
else:
raise SomeError("You cannot make a computation with an unrelated instance")
my thoughts
To be honest, it seems like ValueError
would make most sense because the value is not allowed, but for some reason this does not fully sit well with me. The fact that there is no relation is the importance of this error, not simply the fact that the value attempted is not allowed.
Is there a better exception than ValueError for my logic?
note: I understand that this ValueError
may just be the right answer, but I am curious if there is something more precise that I may have not been able to see the connection with when I went through the documentation.