views:

264

answers:

3

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.

A: 

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.

ChadNC
Did you mean `IllegalStateException` for the second "IllegalArgumentException"? If not, I'm really confused...
ehdv
AFAIK that is not what the OP is asking.
DR
@DR - you would be correct. Thanks for the formatting improvement, btw.
ehdv
+1  A: 

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.

stacker
He's talking about names, not extending the InvalidActivityException.
Bart van Heukelom
+3  A: 

you can have legal usage of an API and still have invalid data.

fuzzy lollipop
But the IllegalArgumentException appears to cover both illegal API usage and invalid data.
ehdv