{"record":{"id":"2d17d608e5323895","repo":"TheAlgorithms/Java","slug":"queue-capacity-must-be-greater-than-0","errorCode":null,"errorMessage":"Queue capacity must be greater than 0","messagePattern":"Queue capacity must be greater than 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/Queue.java","lineNumber":35,"sourceCode":"    private int rear;\n    private int nItems;\n\n    /**\n     * Initializes a queue with a default capacity.\n     */\n    public Queue() {\n        this(DEFAULT_CAPACITY);\n    }\n\n    /**\n     * Constructor to initialize a queue with a specified capacity.\n     *\n     * @param capacity The initial size of the queue.\n     * @throws IllegalArgumentException if the capacity is less than or equal to zero.\n     */\n    public Queue(int capacity) {\n        if (capacity <= 0) {\n            throw new IllegalArgumentException(\"Queue capacity must be greater than 0\");\n        }\n        this.maxSize = capacity;\n        this.queueArray = new Object[capacity];\n        this.front = 0;\n        this.rear = -1;\n        this.nItems = 0;\n    }\n\n    /**\n     * Inserts an element at the rear of the queue.\n     *\n     * @param element Element to be added.\n     * @return True if the element was added successfully, false if the queue is full.\n     */\n    public boolean insert(T element) {\n        if (isFull()) {\n            return false;\n        }","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/Queue.java#L17-L53","documentation":"Thrown by the Queue(int capacity) constructor as an IllegalArgumentException when capacity <= 0. The array-backed queue allocates queueArray = new Object[capacity] immediately, so a non-positive capacity is impossible to allocate and is rejected up front.","triggerScenarios":"Passing 0 or a negative number to new Queue<>(capacity); passing a computed capacity (e.g. from config, a list size, or a formula) that evaluates to zero or below under some input.","commonSituations":"Config value for queue size left blank/zero; capacity derived as (someCount - buffer) that goes negative when input is small; deserialized/defaulted settings producing 0; copy-paste of a default constant that was never set.","solutions":["Validate capacity > 0 before construction and fall back to a sane default (e.g. the no-arg Queue() which uses DEFAULT_CAPACITY).","Clamp computed capacities: Math.max(1, computed).","Treat a zero/negative capacity from config as a fatal misconfiguration and fail startup with a clear message."],"exampleFix":"// before\nQueue<T> q = new Queue<>(configuredCapacity); // may be <= 0\n\n// after\nint cap = Math.max(1, configuredCapacity);\nQueue<T> q = new Queue<>(cap);","handlingStrategy":"validation","validationCode":"int cap = Math.max(1, configuredCapacity);\nQueue<T> q = new Queue<>(cap);","typeGuard":"boolean validCapacity = configuredCapacity > 0;","tryCatchPattern":"null","preventionTips":["Clamp or validate capacity at the config boundary, before it reaches the constructor.","Prefer the no-arg Queue() (DEFAULT_CAPACITY) when size is unknown.","Fail startup loudly if a required capacity config is non-positive rather than letting it throw at construction."],"tags":["queue","data-structure","constructor","capacity","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}