{"record":{"id":"2a3a951485b0f6d2","repo":"TheAlgorithms/Java","slug":"queue-is-empty-2a3a95","errorCode":null,"errorMessage":"Queue is Empty","messagePattern":"Queue is Empty","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java","lineNumber":128,"sourceCode":"     */\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\n            // Swap max and last element\n            int temp = queueArray[1];\n            queueArray[1] = queueArray[nItems];\n            queueArray[nItems] = temp;\n            queueArray[nItems--] = 0; // Nullify the last element from the priority queue\n            sink(1); // Sink the element in order\n\n            return max;\n        }\n    }\n\n    /**\n     * Checks what's at the front of the queue\n     *","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java#L110-L146","documentation":"Thrown by PriorityQueue.remove() as a RuntimeException(\"Queue is Empty\") when isEmpty(). The method extracts the max-priority element (heap root at index 1) and re-heapifies; with nItems == 0 there is no root to return. The thrown type is the generic RuntimeException, not NoSuchElementException.","triggerScenarios":"Calling remove() on a freshly constructed PriorityQueue, or calling remove() more times than insert(). Any extraction after the heap is drained throws.","commonSituations":"Consumer draining the priority queue before any inserts; off-by-one loop bound; tests calling remove without seeding; callers only catching specific exception types miss the generic RuntimeException.","solutions":["Guard with isEmpty() before remove() and skip or wait when empty.","Bound removal loops by the known insert count or by re-checking isEmpty() each iteration.","Catch RuntimeException around remove() since the type is generic rather than a standard underflow exception."],"exampleFix":"// before\nint max = pq.remove(); // throws RuntimeException when empty\n\n// after\nint max = -1;\nif (!pq.isEmpty()) {\n    max = pq.remove();\n}","handlingStrategy":"validation","validationCode":"if (!pq.isEmpty()) {\n    int max = pq.remove();\n}","typeGuard":"null","tryCatchPattern":"try { int max = pq.remove(); } catch (RuntimeException e) { /* empty handling */ }","preventionTips":["isEmpty()-guard remove before each extraction.","Since remove throws generic RuntimeException (not NoSuchElementException), catch Exception if you must handle it.","Bound removal loops by the actual insert count."],"tags":["priority-queue","data-structure","underflow","empty-state","heap","runtime-exception","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}