Commit 97a79ec
committed
feat: Implement custom stack with StackOverFlow and StackUnderFlow exceptions
WHAT the code does:
- Defines two custom exception classes:
- StackOverFlow → thrown when pushing into a full stack.
- StackUnderFlow → thrown when popping from an empty stack.
- Implements Stack class:
- Uses an integer array S[] with fixed capacity.
- Maintains size and top index (initialized to -1).
- push(int x):
- If stack is full (top == size - 1), throws StackOverFlow.
- Otherwise, increments top and stores the element.
- pop():
- If stack is empty (top == -1), throws StackUnderFlow.
- Otherwise, returns the top element and decrements top.
- Demonstrates usage in StackOverAndUnderFlow main():
- Creates a stack of size 5.
- Pushes six elements, triggering StackOverFlow.
- Exception is caught and printed.
WHY this matters:
- Shows how to implement a stack data structure with robust error handling.
- Demonstrates custom exceptions for domain-specific errors, instead of relying on generic runtime exceptions.
- Reinforces defensive programming: preventing invalid operations before they cause silent failures or array out-of-bounds errors.
HOW it works:
1. Stack(5) → initializes an empty stack with capacity 5.
2. Push operations increment top and fill stack until it reaches index 4.
3. On the 6th push, condition top == size - 1 evaluates true → throws StackOverFlow.
4. catch(StackOverFlow) prints "Stack is Full".
Tips and gotchas:
- Always define clear custom exceptions when domain rules are broken (stack overflow/underflow are classic examples).
- push() and pop() can be tested separately:
- Call pop() immediately on a new stack to trigger StackUnderFlow.
- Call push() beyond capacity to trigger StackOverFlow.
- In real-world systems, consider using java.util.Stack or Deque, but writing it manually builds deep understanding.
- Avoid hardcoding "5"; instead make size dynamic or user-defined.
Use-cases / analogies:
- Undo/redo operations in text editors (stack of actions).
- Balanced parentheses checking in compilers.
- Function call stack simulation in interpreters.
- Browser back/forward navigation history.
Short key: java stack custom-exception overflow-underflow defensive-programming.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent eaf9249 commit 97a79ec
1 file changed
+12
-74
lines changedLines changed: 12 additions & 74 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
| 2 | + | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 7 | + | |
| 8 | + | |
14 | 9 | | |
15 | 10 | | |
16 | 11 | | |
17 | 12 | | |
18 | | - | |
19 | | - | |
| 13 | + | |
20 | 14 | | |
21 | 15 | | |
22 | 16 | | |
23 | 17 | | |
24 | | - | |
25 | | - | |
| 18 | + | |
26 | 19 | | |
27 | 20 | | |
28 | 21 | | |
29 | 22 | | |
30 | | - | |
31 | | - | |
| 23 | + | |
32 | 24 | | |
33 | 25 | | |
34 | 26 | | |
35 | 27 | | |
36 | 28 | | |
37 | 29 | | |
38 | | - | |
39 | | - | |
| 30 | + | |
40 | 31 | | |
41 | 32 | | |
42 | 33 | | |
| |||
47 | 38 | | |
48 | 39 | | |
49 | 40 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 41 | + | |
| 42 | + | |
54 | 43 | | |
55 | | - | |
56 | | - | |
| 44 | + | |
57 | 45 | | |
58 | 46 | | |
59 | 47 | | |
60 | 48 | | |
61 | 49 | | |
62 | 50 | | |
63 | | - | |
64 | 51 | | |
65 | | - | |
66 | | - | |
| 52 | + | |
67 | 53 | | |
68 | 54 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | 55 | | |
88 | 56 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
0 commit comments