Reputation: 11450
Is there an existing Java exception that can be thrown when an attempt is made to add an item to a fixed-size custom collection, where the operation would cause the collection to exceed its size? The collection is a form of queue, so the concept of "index" isn't exposed by its interface, otherwise I'd use IndexOutOfBoundsException.
Upvotes: 1
Views: 123
Reputation: 198133
ArrayBlockingQueue
in the JDK already has this use case, and it throws IllegalStateException
in the case where the collection is already full.
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#add(E)
Throws: IllegalStateException - if this queue is full
So IllegalStateException
is the exception Java already uses in this case.
Upvotes: 3
Reputation: 13930
You could also throw an IllegalArgumentException
simply because, as the docs state, it is...
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
which would also be technically correct given the collection and method is well documented. It would then be assumed to be the client's responsibility to check capacity before inserting.
Upvotes: 1
Reputation: 46
You can manually throw IndexOutOfBoundsException.
throw new IndexOutOfBoundsException("your message goes here");
Upvotes: 1