{"record":{"id":"71fa4484dc9d1ea8","repo":"TheAlgorithms/Java","slug":"cannot-delete-from-an-empty-heap","errorCode":null,"errorMessage":"Cannot delete from an empty heap","messagePattern":"Cannot delete from an empty heap","errorType":"exception","errorClass":"EmptyHeapException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java","lineNumber":202,"sourceCode":"    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public void insertElement(HeapElement element) {\n        if (element == null) {\n            throw new IllegalArgumentException(\"Cannot insert null element\");\n        }\n        maxHeap.add(element);\n        toggleUp(maxHeap.size());\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public void deleteElement(int elementIndex) throws EmptyHeapException {\n        if (maxHeap.isEmpty()) {\n            throw new EmptyHeapException(\"Cannot delete from an empty heap\");\n        }\n        if ((elementIndex > maxHeap.size()) || (elementIndex <= 0)) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + maxHeap.size() + \"]\");\n        }\n\n        // Replace with last element and remove last position\n        maxHeap.set(elementIndex - 1, maxHeap.getLast());\n        maxHeap.removeLast();\n\n        // No need to toggle if we just removed the last element\n        if (!maxHeap.isEmpty() && elementIndex <= maxHeap.size()) {\n            // Determine whether to toggle up or down\n            if (elementIndex > 1 && getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2.0))) {\n                toggleUp(elementIndex);\n            } else {\n                toggleDown(elementIndex);\n            }\n        }","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java#L184-L220","documentation":"Thrown by MaxHeap.deleteElement(int) when the heap is empty. deleteElement swaps the target with the last element and removes it; with zero elements there is nothing to delete and the subsequent index check would be meaningless. This is a checked EmptyHeapException that callers must handle or declare.","triggerScenarios":"Calling deleteElement before any insert; calling delete after the heap was fully drained; a teardown routine that deletes unconditionally.","commonSituations":"Cleanup/teardown paths assuming occupancy; cancellation flows removing a job from an already-empty queue; tests with no setup.","solutions":["Guard with an emptiness check before calling deleteElement.","Handle the checked EmptyHeapException at the call site (it is often a benign 'nothing to do' case).","Track the element count externally and skip deletion when it is zero."],"exampleFix":"// before\nheap.deleteElement(index);\n\n// after\ntry {\n    heap.deleteElement(index);\n} catch (EmptyHeapException ignored) {\n    // heap already empty; nothing to delete\n}","handlingStrategy":"try-catch","validationCode":"// If a public emptiness check is available, guard first:\n// if (!heap.isEmpty()) heap.deleteElement(index);","typeGuard":null,"tryCatchPattern":"try {\n    heap.deleteElement(index);\n} catch (EmptyHeapException e) {\n    // heap already empty; nothing to delete\n}","preventionTips":["Guard deletion paths that may run on an empty heap.","Treat EmptyHeapException as benign in teardown/cleanup code.","Track occupancy externally and short-circuit deletion when count is zero."],"tags":["heap","max-heap","empty-state","checked-exception","delete"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}