java - which exception to throw when user tries to add to a full container -
i trying write fixed sized queue generic type elements.the user can call constructor, providing size , internal array created size.(see below)
class fixedsizeq<e>{ private e[] q; private int n; public fixedsizeq(int max) { super(); q = (e[]) new object[max]; n = 0; } .. }
i thought of defining isempty() , isfull() methods , insert() method above.if client tries add element full array,i want throw exception.going through javadocs,i thought illegalstateexception
correct exception throw.
public boolean isfull(){ return n == q.length; } public void insert(item item){ if(isfull()){ throw new illegalstateexception("queue full"); } ... }
i want know if understanding correct..someone suggested illegalargumentexception
more appropriate.can advise?
i think should make insert method return boolean, , return true if object inserted , false if queue full. adding objects full queue not seem exceptional condition me.
in case, if no predefined exception in java api match, can create exception type of own best suit situation:
public class queuefullexception extends runtimeexception { // or "extends exception" if want checked ... }
Comments
Post a Comment