{"record":{"id":"18947f2febee03bd","repo":"TheAlgorithms/Java","slug":"queue-is-full-18947f","errorCode":null,"errorMessage":"Queue is full","messagePattern":"Queue is full","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java","lineNumber":114,"sourceCode":"            }\n\n            // If not exchange the value of parent with child\n            int temp = queueArray[pos];\n            queueArray[pos] = queueArray[current];\n            queueArray[current] = temp;\n            pos = current; // Exchange parent position to child position in the array\n        }\n    }\n\n    /**\n     * Inserts an element in it's appropriate place\n     *\n     * @param value Value to be inserted\n     */\n    public void insert(int value) {\n        // Print overflow message if the capacity is full\n        if (isFull()) {\n            throw new RuntimeException(\"Queue is full\");\n        } else {\n            queueArray[++nItems] = value;\n            swim(nItems); // Swim up the element to its correct position\n        }\n    }\n\n    /**\n     * Dequeue the element with the max priority from PQ\n     *\n     * @return The element removed\n     */\n    public int remove() {\n        if (isEmpty()) {\n            throw new RuntimeException(\"Queue is Empty\");\n        } else {\n            int max = queueArray[1]; // By definition of our max-heap, value at queueArray[1] pos is\n                                     // the greatest\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java#L96-L132","documentation":"Thrown by PriorityQueue.insert(int) as a RuntimeException(\"Queue is full\") when isFull(). This is an array-backed max-heap whose capacity is fixed at construction (size+1 slots, with slot 0 unused). Once the heap holds the configured number of items, further inserts are rejected. Note the generic RuntimeException, not a more specific type.","triggerScenarios":"Calling insert(value) more than the configured capacity times without remove() calls. Capacity is the size passed to the constructor (default 11); the array is allocated as size+1 but usable capacity equals size.","commonSituations":"Fixed-capacity priority queue overwhelmed by ingest rate; capacity mis-sized because the +1 internal slot led to an off-by-one in mental model; feed loop with no removal step; RuntimeException not caught because callers only expected checked exceptions.","solutions":["Guard with isFull() (or track nItems against capacity) before insert().","Size the constructor capacity to the peak number of concurrent items.","Remove lower-priority elements before inserting new ones to keep occupancy below capacity.","Catch RuntimeException (or the more general Exception) around insert since the type is not specific."],"exampleFix":"// before\npq.insert(value); // throws RuntimeException when full\n\n// after\nif (pq.isFull()) {\n    pq.remove(); // evict lowest priority to make room\n}\npq.insert(value);","handlingStrategy":"validation","validationCode":"if (!pq.isFull()) {\n    pq.insert(value);\n} else {\n    pq.remove(); // evict lowest priority\n    pq.insert(value);\n}","typeGuard":"null","tryCatchPattern":"try { pq.insert(value); } catch (RuntimeException e) { /* capacity handling */ }","preventionTips":["Guard insert with isFull(); capacity equals the constructor size (array is size+1 but slot 0 is unused).","Because the throw is a generic RuntimeException, callers must catch broadly, not a specific type.","Evict lower-priority items before inserting when capacity is tight."],"tags":["priority-queue","data-structure","capacity","bounded-queue","heap","runtime-exception","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}