Commit 2556858
committed
feat: Implement custom checked exception LowBalanceException with try–catch handling
WHAT the code does:
- Defines LowBalanceException:
- Extends Exception (checked exception).
- Overrides toString() to return a custom message: "Balance Should not be less than 5000".
- Defines LowBankBalanceTrycatch class with three static methods:
- fun1():
- Explicitly throws new LowBalanceException.
- Immediately catches it in catch block.
- Prints exception using overridden toString().
- fun2(): calls fun1().
- fun3(): calls fun2().
- main():
- Calls fun3(), which cascades down to fun1() where exception is thrown and handled.
WHY this matters:
- Demonstrates creation and usage of a **custom exception class** for domain-specific error handling.
- Shows how checked exceptions require explicit handling, either with try–catch or throws.
- Illustrates exception propagation and how exceptions can be thrown and caught within the same method.
HOW it works:
1. main() calls fun3() → fun2() → fun1().
2. fun1():
- Executes `throw new LowBalanceException()`.
- Immediately caught in catch block.
- System.out.println(e) → calls LowBalanceException.toString() → prints "Balance Should not be less than 5000".
3. Program ends gracefully.
Tips and gotchas:
- Typically, user-defined exceptions are **thrown up the call stack** to be handled by calling code, rather than caught immediately where they are thrown. This demo keeps it local for simplicity.
- Best practice: include a constructor in custom exceptions that accepts a message or cause, so error context can be passed.
- Extending RuntimeException instead of Exception would make it unchecked (no need for try–catch at compile time).
- Method naming conventions: fun1(), fun2(), fun3() should be replaced with descriptive names in production.
Use-cases / analogies:
- LowBalanceException models banking rules: accounts must maintain a minimum balance (e.g., 5000).
- Real-world: throwing InsufficientFundsException when a withdrawal would drop balance below threshold.
Short key: java custom-exception checked low-balance banking-rules exception-handling.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e23e412 commit 2556858
File tree
2 files changed
+12
-47
lines changed- Section18ExceptionHandling/src
2 files changed
+12
-47
lines changedThis file was deleted.
Lines changed: 12 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 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 | | - | |
30 | | - | |
| 22 | + | |
31 | 23 | | |
32 | 24 | | |
33 | 25 | | |
34 | | - | |
35 | | - | |
| 26 | + | |
36 | 27 | | |
37 | 28 | | |
38 | | - | |
| 29 | + | |
0 commit comments