{"record":{"id":"b9a9937928d3b121","repo":"TheAlgorithms/Java","slug":"queue-is-empty","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java","lineNumber":94,"sourceCode":"            throw new IllegalStateException(\"Queue is full\");\n        }\n        if (isEmpty()) {\n            beginningOfQueue = 0;\n        }\n        topOfQueue = (topOfQueue + 1) % size;\n        array[topOfQueue] = value;\n        currentSize++;\n    }\n\n    /**\n     * Removes and returns the element at the front of the queue.\n     *\n     * @return the element at the front of the queue\n     * @throws IllegalStateException if the queue is empty\n     */\n    public T deQueue() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Queue is empty\");\n        }\n        T removedValue = array[beginningOfQueue];\n        array[beginningOfQueue] = null; // Optional: Nullify to help garbage collection\n        beginningOfQueue = (beginningOfQueue + 1) % size;\n        currentSize--;\n        if (isEmpty()) {\n            beginningOfQueue = -1;\n            topOfQueue = -1;\n        }\n        return removedValue;\n    }\n\n    /**\n     * Returns the element at the front of the queue without removing it.\n     *\n     * @return the element at the front of the queue\n     * @throws IllegalStateException if the queue is empty\n     */","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java#L76-L112","documentation":"Thrown by CircularQueue.deQueue() as an IllegalStateException when isEmpty() is true (currentSize == 0). The queue refuses to return a front element it does not have. This is the standard underflow guard for a bounded ring buffer.","triggerScenarios":"Calling deQueue() on a freshly constructed CircularQueue, or calling deQueue() more times than enQueue() has been called. Also triggered after deleteQueue() resets state if the instance is still referenced.","commonSituations":"Consumer draining a queue the producer has not yet filled; off-by-one loop where the dequeue count exceeds the enqueue count; draining logic running once more after the queue is exhausted; shared queue where one consumer races ahead of production.","solutions":["Call isEmpty() (or isFull() check) before deQueue() and skip or wait when the queue is empty.","Track enqueued vs dequeued counts in the caller to prevent over-dequeue in loops.","Wrap the call in try/catch(IllegalStateException) if the empty case should be handled as a normal control-flow signal.","For concurrent use, replace with a blocking queue so the consumer waits instead of throwing."],"exampleFix":"// before\nT v = queue.deQueue(); // throws when empty\n\n// after\nT v = null;\nif (!queue.isEmpty()) {\n    v = queue.deQueue();\n}","handlingStrategy":"validation","validationCode":"if (!queue.isEmpty()) {\n    T v = queue.deQueue();\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Check isEmpty() before every deQueue on a bounded ring buffer.","Drive drain loops by a count derived from the queue, not an external loop variable.","For concurrent pipelines, prefer a blocking queue so the consumer waits instead of throwing."],"tags":["queue","data-structure","underflow","empty-state","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}