Commit 314a653
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
1 file changed
+69
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
0 commit comments