{"record":{"id":"8e7a66577aaae852","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"queue-is-empty-8e7a66","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/CustomQueue.java","lineNumber":36,"sourceCode":"    public boolean isFull() {\n        return end == data.length; // ptr is at last index\n    }\n\n    public boolean isEmpty() {\n        return end == 0;\n    }\n\n    public boolean insert(int item) {\n        if (isFull()) {\n            return false;\n        }\n        data[end++] = item;\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[0];\n\n        // shift the elements to left\n        for (int i = 1; i < end; i++) {\n            data[i-1] = data[i];\n        }\n        end--;\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[0];\n    }","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/19-stacks-n-queues/code/src/com/kunal/CustomQueue.java#L18-L54","documentation":"CustomQueue.remove() throws a checked Exception when the queue is empty, since there is no element at data[0] to dequeue. The checked throws declaration makes callers explicitly deal with underflow.","triggerScenarios":"Calling remove() on a CustomQueue where isEmpty() is true: dequeuing more than insert() was called, or removing from a new queue.","commonSituations":"BFS/processing loops calling remove() one time too many; producer/consumer code where the consumer outruns the producer; reusing a queue after draining it without checking size.","solutions":["Wrap the call: if (!queue.isEmpty()) { queue.remove(); }.","Catch Exception around remove() and handle gracefully.","Track consumed counts against insert() counts in loops."],"exampleFix":"// before\nint item = queue.remove();\n// after\nwhile (!queue.isEmpty()) {\n    int item = queue.remove();\n}","handlingStrategy":"validation","validationCode":"if (!queue.isEmpty()) {\n    int item = queue.remove();\n}","typeGuard":null,"tryCatchPattern":"try {\n    int item = queue.remove();\n} catch (Exception e) {\n    // queue underflow — skip or terminate the loop\n}","preventionTips":["Check isEmpty() before every remove().","Bounds-check loops that dequeue a known count of items.","Synchronize producer/consumer assumptions or check size each iteration.","Reset size tracking when reusing queue objects."],"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"}