{"record":{"id":"c5f928e2c56c102c","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"cannot-peek-from-an-empty-stack","errorCode":null,"errorMessage":"Cannot peek from an empty stack!!","messagePattern":"Cannot peek from an empty stack!!","errorType":"exception","errorClass":"StackException","httpStatus":null,"severity":"error","filePath":"lectures/19-stacks-n-queues/code/src/com/kunal/CustomStack.java","lineNumber":39,"sourceCode":"        }\n        ptr++;\n        data[ptr] = item;\n        return true;\n    }\n\n    public int pop() throws StackException {\n        if (isEmpty()) {\n            throw new StackException(\"Cannot pop from an empty stack!!\");\n        }\n//        int removed = data[ptr];\n//        ptr--;\n//        return removed;\n        return data[ptr--];\n    }\n\n    public int peek() throws StackException {\n        if (isEmpty()) {\n            throw new StackException(\"Cannot peek from an empty stack!!\");\n        }\n        return data[ptr];\n    }\n\n    public boolean isFull() {\n        return ptr == data.length - 1; // ptr is at last index\n    }\n\n    public boolean isEmpty() {\n        return ptr == -1;\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":52,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/19-stacks-n-queues/code/src/com/kunal/CustomStack.java#L21-L52","documentation":"CustomStack.peek() throws a custom StackException when the stack is empty, because data[ptr] holds no valid element. Unlike pop(), peek does not modify the stack; the exception purely signals underflow.","triggerScenarios":"Calling peek() on an empty stack: peeking before the first push, or after all elements were popped — e.g. top comparisons in isValid-style code once the stack is drained.","commonSituations":"Peek-driven comparisons ('is top greater than x') that run when no elements exist; debug/logging of the top value; iterative algorithms peeking in the loop condition without an emptiness check.","solutions":["Guard with if (!stack.isEmpty()) before peek().","Catch StackException and provide a default value.","Combine peek+pop into one guarded block sharing the emptiness check."],"exampleFix":"// before\nint top = stack.peek();\n// after\nint top = stack.isEmpty() ? -1 : stack.peek();","handlingStrategy":"validation","validationCode":"int top = stack.isEmpty() ? -1 : stack.peek();","typeGuard":null,"tryCatchPattern":"try {\n    int top = stack.peek();\n} catch (StackException e) {\n    int top = -1; // empty-stack default\n}","preventionTips":["Check isEmpty() before peek().","Handle the 'stack just drained' case explicitly in comparison loops.","Peek only after at least one push has executed.","Return sentinel values instead of throwing when peeking optionally."],"tags":["java","stack","peek","custom-exception"],"backgroundTag":"stack-empty-underflow","analyzedSha":"6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c","analyzedAt":"2026-08-31T22:04:22.314Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}