Reputation: 4603
In Java, is there a semantic difference between using "Illegal" (as in IllegalArgumentException
) versus "Invalid" (as in javax.activity.InvalidActivityException
)?
During the course of an assignment it became useful to write a subclass of IllegalArgumentException
to represent a series of input characters that cannot be tokenized, and I'm wondering whether convention says to use InvalidTokenException
or IllegalTokenException
.
The only difference I can find so far is that java.lang
seems to prefer "Illegal" while javax.*
prefers "Invalid". However, there is also java.security.InvalidParameterException
which is a subclass of IllegalArgumentException
.
Upvotes: 24
Views: 8923
Reputation: 21466
I was just pondering the same question. There is no documentation I know of explaining any distinction between "illegal" and "invalid". If I were try to tease out some semantics, perhaps "illegal" could be considered a broader category meaning "not permitted", while "invalid" represents some value that doesn't meet some validation constraints or isn't recognized.
Thus in one of those old text-base adventure games, turn("qwerty")
would pass a value that is both illegal and invalid, as "qwerty"
is not a valid direction to turn. However turn("left")
would represent a valid value, which might or might not be a "legal" direction to turn depending on whether there was a wall to the left of the current player's position.
In that light, IllegalArgumentException
represents any argument that is not-permitted for whatever reason, whether the value in itself is otherwise valid.
I stress that this explanation is something completely of my own reasoning, and may or may not reflect established conventions. But until someone comes up with a better conceptualization, this is the approach I'm going to take in my libraries.
See also the discussion at illegal vs invalid, which provisionally makes a distinction similar to the one I just made. It also points out that those who chose the existing usages "are not always sensitive to shades of meaning" to begin with. Thus I would imagine that in many cases 'illegal" and "invalid" were used interchangeably.
Upvotes: 0
Reputation:
You can have legal usage of an API and still have invalid data; it is all semantics.
Upvotes: 13
Reputation: 68992
javax.activity.InvalidActivityException
is inherited from java.rmi.RemoteException
and you probably don't want this dependency. See also Javadoc
EDIT both Invalid and Illegal are used synonymously it makes no differency in semantics, just the technical issues mentioned above.
EDIT: From Postgres Documentation Section 45.3.14. Tricky words to avoid:
Illegal. "Illegal" stands for a violation of the law, the rest is "invalid". Better yet, say why it's invalid.
Upvotes: 2
Reputation: 2503
AFAIK, IllegalArgumentException should only be used when you want to signal the incorrect use of an API method call. What it appears you are describing is a scenario when the API has been used incorrectly, so I think an IllegalArgumentException would be the better choice.
Upvotes: -1