Skip to content

Commit 2556858

Browse files
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

2 files changed

+12
-47
lines changed

Section18ExceptionHandling/src/LinksOpen.cmd

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1-
//This is the user defined exceptions
2-
3-
class LowBalanceException extends Exception
4-
{
5-
public String toString()
6-
{
1+
class LowBalanceException extends Exception {
2+
//This is the user-defined exceptions
3+
public String toString() {
74
return "Balance Should not be less than 5000";
85
}
96
}
10-
public class LowBankBalanceTrycatch
11-
{
12-
static void fun1()
13-
{
14-
try
15-
{
7+
8+
public class LowBankBalanceTrycatch {
9+
static void fun1() {
10+
try {
1611
throw new LowBalanceException();
1712
}
18-
catch(LowBalanceException e)
19-
{
13+
catch(LowBalanceException e) {
2014
System.out.println(e);
2115
}
2216
}
2317

24-
static void fun2()
25-
{
18+
static void fun2() {
2619
fun1();
2720
}
2821

29-
static void fun3()
30-
{
22+
static void fun3() {
3123
fun2();
3224
}
3325

34-
public static void main(String[] args)
35-
{
26+
public static void main(String[] args) {
3627
fun3();
3728
}
38-
}
29+
}

0 commit comments

Comments
 (0)