NOTE: This question is about finding a proper name for an exception where the condition for throwing the exception has not actually happened (eg. preventing a StackOverflow by throwing a StackOverflowException
before an overflow can occur)
I have a FooList
class that has bounds on the number of elements it can contain. The constructor takes an initial list of elements, and more elements can be added, up to the limit.
The question I have is this. Having a full FooList
is not exceptional. I decided that it would probably be best if I provided the client with a way of checking to see if there was room in the FooList
before trying to add something to it (hasRoom
). However, this doesn't really stop them from calling add
without checking first.
I decided that in this case, it may be appropriate to throw an exception. However, I'm not exactly sure what to call it. As I stated, having a full FooList
is not particularly exceptional, and is in fact a perfectly valid state. So throwing a FooListFullException
doesn't feel right. However, throwing an UnableToAddFooToListBecauseFooListIsFullException
also doesn't feel right.
public class FooList {
FooList(List<Foo> foos) {
Objects.requireNonNull(foos);
if (foos.size() > 6) {
throw new IllegalArgumentException("foos must not contain more than 6 Foos");
}
if (foos.size() < 1) {
throw new IllegalArgumentException("foos must contain at least 1 Foo");
}
this.foos = foos;
}
void add(Foo foo) {
Objects.requireNonNull(foo);
if (!hasRoom()) {
throw new XException("no more room in list");
}
}
boolean hasRoom() {
return getNextEmptyIndex() != -1;
}
private List<Foo> foos;
}
In what way could I name the exception to indicate that an add
operation is unable to complete, OR, is there a better way to get this across to the user/client?
Optional<Settable<T>>
which would avoid race conditions though another thread could still observe an unset element.