I have a class that is supposed to wrap around strings found in some input file. The contract is that this files contains multiple entries (separated by # chars), to be precise: at least two entries should come out of that file.
Now I could do this:
public MyWrapper(List<String> fileEntries) {
if (fileEntries.size() < 2) {
throw new IllegalArgumentException("Not enough entries ...
}
or I could just do:
public MyWrapper(List<String> fileEntries) {
this.firstEntry = fileEntries.get(0);
this.secondEntry = someMethodThatMergesAllRemainingEntries(fileEntries);
That second piece would throw ArrayIndexOutOfBound at some point.
My question is: is there some precedent in the Java standard library that could be seen as guidance? (similar to the Java standard library call requireNonNull() that tells us to maybe prefer to throw a NPE instead of IllegalArgumentException for null arguments).
Note: I am not asking "what would be better", both sides have their pros and cons (I am slightly tempted to prefer option 1, as that is simply shorter).