Skip to content

Commit 314a653

Browse files
committed
feat: Add stack implementation with runtime exceptions for overflow and underflow
WHAT the code does: - Defines two custom runtime exceptions: - StackOverFlow1 extends IllegalStateException → thrown when pushing into a full stack. - StackUnderFlow1 extends IllegalStateException → thrown when popping from an empty stack. - Implements StackOverUnder: - Uses a fixed-size integer array (values) to hold elements. - count keeps track of the number of elements. - push(int x): - Throws StackOverFlow1 if stack is full (count == capacity). - Otherwise, stores the value and increments count. - pop(): - Throws StackUnderFlow1 if stack is empty (count == 0). - Otherwise, decrements count and returns the last pushed value. - getCapacity() returns the fixed size of the stack. - Demonstrates usage in TestStack: - Creates a stack with capacity 2. - Tries to push three elements → triggers StackOverFlow1. - Pops three times from a two-element stack → triggers StackUnderFlow1. WHY this matters: - Shows how to implement a robust stack with runtime (unchecked) exceptions. - Highlights the difference between checked vs unchecked custom exceptions: - Here exceptions extend IllegalStateException (unchecked) → no need for throws declaration. - Demonstrates how stack operations should enforce boundaries to avoid invalid states. HOW it works: 1. push(10), push(20) succeed. 2. push(30) fails since count == capacity → throws StackOverFlow1. 3. pop() twice returns 20 and 10. 4. Third pop() fails since stack is empty → throws StackUnderFlow1. 5. Each exception is caught and printed using overridden toString(). Tips and gotchas: - Runtime exceptions are suitable for programming errors (like exceeding stack capacity), while checked exceptions are better for recoverable conditions. - Use IllegalStateException as a parent for logical state errors (stack full/empty is a state violation). - For thread-safety in concurrent contexts, use synchronization or java.util.concurrent structures. - In production, prefer java.util.Deque (e.g., ArrayDeque) for stack-like behavior unless educational purpose requires manual implementation. Use-cases / analogies: - Stack data structure is fundamental: - Function call stack in programming languages. - Undo/redo operations in applications. - Evaluating arithmetic expressions in compilers. - This example demonstrates how runtime validation prevents illegal operations in these contexts. Short key: java stack runtime-exception overflow underflow illegal state. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9129a32 commit 314a653

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Custom Exception for Stack Overflow.
2+
class StackOverFlow1 extends IllegalStateException {
3+
@Override
4+
public String toString() {
5+
return "Stack is Full";
6+
}
7+
}
8+
9+
// Custom Exception for Stack Underflow.
10+
class StackUnderFlow1 extends IllegalStateException {
11+
@Override
12+
public String toString() {
13+
return "Stack is Empty";
14+
}
15+
}
16+
17+
class StackOverUnder {
18+
private final int[] values;
19+
private int count = 0;
20+
21+
// Constructor to initialize stack with capacity.
22+
public StackOverUnder(int capacity) {
23+
values = new int[capacity];
24+
}
25+
26+
public int getCapacity() {
27+
return values.length;
28+
}
29+
30+
// Push method.
31+
public void push(int x) {
32+
if (count == values.length) {
33+
throw new StackOverFlow1();
34+
}
35+
values[count] = x;
36+
++count;
37+
}
38+
39+
// Pop method.
40+
public int pop() {
41+
if (count == 0) {
42+
throw new StackUnderFlow1();
43+
}
44+
--count;
45+
return values[count];
46+
}
47+
}
48+
49+
public class TestStack {
50+
public static void main(String[] args) {
51+
StackOverUnder stack = new StackOverUnder(2);
52+
53+
try {
54+
stack.push(10);
55+
stack.push(20);
56+
stack.push(30); // This will throw StackOverFlow1
57+
} catch (StackOverFlow1 e) {
58+
System.out.println(e);
59+
}
60+
61+
try {
62+
System.out.println(stack.pop()); // 20
63+
System.out.println(stack.pop()); // 10
64+
System.out.println(stack.pop()); // This will throw StackUnderFlow1
65+
} catch (StackUnderFlow1 e) {
66+
System.out.println(e);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)