{"record":{"id":"b6a62d5acaeebe28","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"queue-is-empty","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"lectures/19-stacks-n-queues/code/src/com/kunal/CircularQueue.java","lineNumber":39,"sourceCode":"    }\n\n    public boolean isEmpty() {\n        return size == 0;\n    }\n\n    public boolean insert(int item) {\n        if (isFull()) {\n            return false;\n        }\n        data[end++] = item;\n        end = end % data.length;\n        size++;\n        return true;\n    }\n\n    public int remove() throws Exception {\n        if (isEmpty()) {\n            throw new Exception(\"Queue is empty\");\n        }\n\n        int removed = data[front++];\n        front = front % data.length;\n        size--;\n        return removed;\n    }\n\n    public int front() throws Exception{\n        if (isEmpty()) {\n            throw new Exception(\"Queue is empty\");\n        }\n        return data[front];\n    }\n\n    public void display() {\n        if (isEmpty()) {\n            System.out.println(\"Empty\");","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/19-stacks-n-queues/code/src/com/kunal/CircularQueue.java#L21-L57","documentation":"CircularQueue.remove() throws a checked java.lang.Exception when the queue is empty, because there is no element to dequeue. The throws clause forces callers to handle the underflow condition at compile time.","triggerScenarios":"Calling remove() on a CircularQueue where size == 0 (isEmpty() true): dequeuing more times than insert() was called, or removing before any insert.","commonSituations":"Consumer loops draining a queue faster than producers fill it; off-by-one loop bounds consuming one extra element; calling remove() on a freshly constructed queue.","solutions":["Guard with if (!queue.isEmpty()) before calling remove().","Catch Exception around remove() and treat empty as a normal condition.","Use queue.size()/isEmpty() in loop conditions instead of a fixed count."],"exampleFix":"// before\nint val = queue.remove();\n// after\nif (!queue.isEmpty()) {\n    int val = queue.remove();\n}","handlingStrategy":"validation","validationCode":"if (!cq.isEmpty()) {\n    int val = cq.remove();\n}","typeGuard":null,"tryCatchPattern":"try {\n    int val = cq.remove();\n} catch (Exception e) {\n    // queue was empty — treat as normal, e.g. break the consume loop\n}","preventionTips":["Check isEmpty() before every dequeue.","Drive consume loops off queue.size(), not fixed counts.","Never assume a producer has already enqueued before consuming.","Treat empty-queue as an expected state, not a crash."],"tags":["java","queue","underflow","checked-exception"],"backgroundTag":"queue-empty-underflow","analyzedSha":"6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c","analyzedAt":"2026-08-31T22:04:22.314Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}