{"record":{"id":"11043db69ee1c8f1","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"cannot-pop-from-an-empty-stack","errorCode":null,"errorMessage":"Cannot pop from an empty stack!!","messagePattern":"Cannot pop 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":29,"sourceCode":"    }\n\n    public CustomStack(int size) {\n        this.data = new int[size];\n    }\n\n    public boolean push(int item) {\n        if (isFull()) {\n            System.out.println(\"Stack is full!!\");\n            return false;\n        }\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","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/19-stacks-n-queues/code/src/com/kunal/CustomStack.java#L11-L47","documentation":"CustomStack.pop() throws a custom StackException when the stack is empty, guarding the data[ptr--] access that would otherwise read a stale slot or go out of bounds. The custom exception signals stack underflow with a clear message.","triggerScenarios":"Calling pop() when isEmpty() is true: popping more times than push was called; matching algorithms like isValid/minAddToMakeValid popping on a mismatch with an empty stack; loops whose bound exceeds the pushed count.","commonSituations":"Bracket-matching algorithms where a closing character arrives with an empty stack; unwinding a stack after processing input without checking size; reusing a stack across iterations after it was drained.","solutions":["Check isEmpty() before each pop(), especially in matching algorithms.","Catch StackException around pop() and treat empty as a normal outcome.","Restructure loops to drive off stack size rather than input length alone."],"exampleFix":"// before\nint top = stack.pop();\n// after\nif (!stack.isEmpty()) {\n    int top = stack.pop();\n} else {\n    // unmatched item — handle\n}","handlingStrategy":"try-catch","validationCode":"if (!stack.isEmpty()) {\n    int top = stack.pop();\n}","typeGuard":null,"tryCatchPattern":"try {\n    int top = stack.pop();\n} catch (StackException e) {\n    // underflow: handle unmatched/absent element\n    System.out.println(e.getMessage());\n}","preventionTips":["Check isEmpty() before every pop(), especially in matching algorithms (isValid, minAddToMakeValid).","In bracket matching, pop only when a matching opener exists on the stack.","Track push count vs pop count in loops.","Drain with while (!stack.isEmpty()) instead of fixed counts."],"tags":["java","stack","underflow","custom-exception"],"backgroundTag":"stack-empty-underflow","analyzedSha":"6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c","analyzedAt":"2026-08-31T22:04:22.314Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}