Skip to content

Commit b1357da

Browse files
committed
feat: Demonstrate checked exceptions vs runtime errors in method chaining
WHAT the code does: - Defines class CheckedException with three static methods: - fun1(): - Commented code shows creation of FileInputStream with "My.txt". - Would throw a **checked exception** (FileNotFoundException), which must be handled with try–catch or declared with throws. - fun2(): calls fun1(). - fun3(): recursively calls itself without a base case, leading to infinite recursion and eventually StackOverflowError. - main(): calls fun3(), causing StackOverflowError at runtime. WHY this matters: - Shows difference between **checked exceptions** and **unchecked errors**: - Checked exceptions: - Caught or declared at compile-time (e.g., FileNotFoundException, SQLException). - Example: FileInputStream requires handling since file might not exist. - Unchecked exceptions/errors: - Occur at runtime, not enforced by compiler (e.g., NullPointerException, StackOverflowError). - fun3 demonstrates uncontrolled recursion leading to StackOverflowError. - Highlights how compiler enforces handling for checked exceptions but allows recursive calls that may crash at runtime. HOW it works: 1. If FileInputStream in fun1 were uncommented: - Compilation fails unless code is wrapped in try–catch or method signature declares `throws FileNotFoundException`. 2. fun3(): - Calls itself infinitely. - JVM stack memory fills up. - Throws StackOverflowError (unchecked, subclass of Error). 3. main(): - Directly triggers fun3(), causing program crash at runtime. Tips and gotchas: - Always handle checked exceptions with try–catch or propagate them with throws. - Recursion requires a base case to terminate; otherwise stack overflow occurs. - StackOverflowError cannot be caught meaningfully for recovery; fix logic instead. - Checked exceptions = compile-time obligation; unchecked exceptions = runtime responsibility. Use-cases / analogies: - Checked exceptions: like airport security requiring you to handle your luggage before boarding (must handle before runtime). - Unchecked errors: like falling asleep while driving — compiler doesn’t stop you, but program (car) crashes at runtime. Short key: java exceptions checked-vs-unchecked fileinputstream stackoverflow recursion. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e989d86 commit b1357da

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import java.io.FileInputStream;
22

3-
public class CheckedException
4-
{
3+
public class CheckedException {
54
static void fun1()
65
{
76
/*
87
Checked Exception FileInputStream f1=new FileInputStream("My.txt");
98
*/
109
}
11-
static void fun2()
12-
{
10+
11+
static void fun2() {
1312
fun1();
1413
}
15-
static void fun3()
16-
{
14+
15+
static void fun3() {
1716
fun3();
1817
}
18+
1919
public static void main(String[] args) {
2020
fun3();
2121
}
22-
}
22+
}

0 commit comments

Comments
 (0)