{"record":{"id":"3fbc4c5139f3f017","repo":"TheAlgorithms/Java","slug":"minpriorityqueue-is-empty-cannot-delete","errorCode":null,"errorMessage":"MinPriorityQueue is empty. Cannot delete.","messagePattern":"MinPriorityQueue is empty\\. Cannot delete\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java","lineNumber":147,"sourceCode":"\n            // Swap with the smallest child\n            int temp = this.heap[k];\n            this.heap[k] = this.heap[minIndex];\n            this.heap[minIndex] = temp;\n\n            k = minIndex; // Move down to the smallest child\n        }\n    }\n\n    /**\n     * Deletes and returns the highest priority value (the minimum) from the queue.\n     *\n     * @return the minimum value from the queue\n     * @throws IllegalStateException if the queue is empty\n     */\n    public int delete() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"MinPriorityQueue is empty. Cannot delete.\");\n        }\n        int min = this.heap[1];\n        this.heap[1] = this.heap[this.size]; // Move last element to the root\n        this.size--;\n        this.sink();\n        return min;\n    }\n}\n","sourceCodeStart":129,"sourceCodeEnd":156,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java#L129-L156","documentation":"Thrown by MinPriorityQueue.delete() when isEmpty() returns true. delete moves the last element to the root, decrements size, and re-heapifies, so an empty queue would underflow the size counter and read heap[0]. The guard blocks the underflow.","triggerScenarios":"Calling delete more times than insert. Draining a queue in a fixed-count loop larger than the population. Deleting from a queue whose producer has not started.","commonSituations":"Loop bound computed from a different source than actual insertions. Consumer running ahead of producer. Off-by-one in drain logic.","solutions":["Loop while `!pq.isEmpty()` rather than a fixed count.","Guard each delete with an isEmpty check.","Track insertions and bound deletions to that count.","Catch IllegalStateException at the consumer and treat it as end-of-stream."],"exampleFix":"// before\nwhile (count-- > 0) { int m = pq.delete(); }\n\n// after\nwhile (!pq.isEmpty()) {\n    int m = pq.delete();\n}","handlingStrategy":"validation","validationCode":"if (!pq.isEmpty()) {\n    return pq.delete();\n}","typeGuard":null,"tryCatchPattern":"try {\n    return pq.delete();\n} catch (IllegalStateException e) {\n    // queue empty — end of stream\n    return OptionalInt.empty();\n}","preventionTips":["Drain with `while (!pq.isEmpty())` rather than a fixed count.","Bound deletes by the number of successful inserts.","Treat IllegalStateException at the consumer as end-of-stream."],"tags":["priority-queue","empty-state","data-structure","precondition","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}