Skip to content

Commit b82266e

Browse files
authored
Update exceptions.md
1 parent e2e5be5 commit b82266e

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

exceptions.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,141 @@ try (Resource1 res1 = new Resource1();
286286

287287
---
288288

289+
## 🃏 Exception Chaining and Return Statement Flow
290+
291+
**Key Rule:** Exceptions thrown in **catch blocks are NOT caught** by subsequent catch blocks in the same try-catch structure. Only exceptions from the **try block** can be caught.
292+
293+
```java
294+
public class FamilyReunion {
295+
public static void main(String[] args) {
296+
try {
297+
callGrandpa(); // This will throw an exception from TRY block
298+
} catch (IndexOutOfBoundsException father) {
299+
System.out.println("Father"); // Step 2: Catches exception from try block
300+
throw new NullPointerException(); // Step 3: Throws NEW exception from CATCH block
301+
} catch (NullPointerException mother) {
302+
System.out.println("Mother"); // ❌ WON'T EXECUTE! Exception not from try block
303+
return;
304+
} catch (Exception grandma) {
305+
System.out.println("Grandma"); // ❌ WON'T EXECUTE! Exception not from try block
306+
} finally {
307+
System.out.println("Sister"); // Step 4: Always executes
308+
}
309+
System.out.println("FAMILY_GATHERING"); // ❌ WON'T EXECUTE! NullPointerException propagates
310+
}
311+
312+
static void callGrandpa() {
313+
System.out.println("Grandpa speaks"); // Step 1: Always executes first
314+
throw new IndexOutOfBoundsException("Grandpa is grumpy!");
315+
}
316+
}
317+
318+
// Output:
319+
// Grandpa speaks
320+
// Father
321+
// Sister
322+
// Exception in thread "main" java.lang.NullPointerException
323+
```
324+
325+
**Step-by-Step Execution Flow:**
326+
327+
```java
328+
// Detailed execution breakdown:
329+
public class FamilyReunion {
330+
public static void main(String[] args) {
331+
try {
332+
callGrandpa(); // Step 1: Call method - throws IndexOutOfBoundsException
333+
} catch (IndexOutOfBoundsException father) {
334+
System.out.println("Father"); // Step 2: Catches exception FROM TRY BLOCK
335+
throw new NullPointerException(); // Step 3: NEW exception FROM CATCH BLOCK
336+
} catch (NullPointerException mother) { // ❌ CANNOT catch exceptions from catch blocks!
337+
System.out.println("Mother"); // NEVER executes - exception not from try block
338+
return;
339+
} catch (Exception grandma) { // ❌ CANNOT catch exceptions from catch blocks!
340+
System.out.println("Grandma"); // NEVER executes - exception not from try block
341+
} finally {
342+
System.out.println("Sister"); // Step 4: Always executes before exception propagates
343+
}
344+
System.out.println("FAMILY_GATHERING"); // NEVER executes - uncaught exception terminates method
345+
}
346+
347+
static void callGrandpa() {
348+
System.out.println("Grandpa speaks"); // Step 1a: Method execution
349+
throw new IndexOutOfBoundsException("Grandpa is grumpy!"); // Step 1b: Exception FROM TRY BLOCK
350+
}
351+
}
352+
```
353+
354+
**🚨 Critical Concept:** **Only exceptions thrown in the TRY block can be caught by catch blocks!**
355+
356+
**Exception Source Matters:**
357+
```java
358+
try {
359+
throw new IOException("From try block"); // ✅ CAN be caught by catch blocks below
360+
} catch (IOException e) {
361+
System.out.println("Caught from try"); // ✅ Executes - exception from try block
362+
throw new SQLException("From catch block"); // ❌ CANNOT be caught by catch blocks below
363+
} catch (SQLException e) {
364+
System.out.println("Caught from catch"); // ❌ NEVER executes - exception from catch block
365+
} catch (Exception e) {
366+
System.out.println("Caught anything"); // ❌ NEVER executes - exception from catch block
367+
}
368+
// SQLException propagates up - uncaught!
369+
```
370+
371+
**Key Exception Catching Rules:**
372+
373+
**✅ Exceptions that CAN be caught:**
374+
```java
375+
try {
376+
throw new RuntimeException("From try block"); // ✅ From try block
377+
methodThatThrows(); // ✅ From method called in try block
378+
} catch (RuntimeException e) {
379+
// Will catch exceptions from try block
380+
}
381+
```
382+
383+
**❌ Exceptions that CANNOT be caught:**
384+
```java
385+
try {
386+
normalMethod(); // No exception from try block
387+
} catch (IOException e) {
388+
throw new SQLException("From catch block"); // ❌ From catch block - won't be caught
389+
} catch (SQLException e) {
390+
// NEVER executes - SQLException came from catch block
391+
} finally {
392+
throw new IllegalStateException("From finally"); // ❌ From finally block - won't be caught
393+
}
394+
```
395+
396+
**Nested Try-Catch Example:**
397+
```java
398+
// To catch exceptions from catch blocks, you need NESTED try-catch:
399+
try {
400+
try {
401+
throw new IOException("Original problem");
402+
} catch (IOException e) {
403+
System.out.println("Caught original");
404+
throw new SQLException("New problem"); // This gets thrown to outer try
405+
}
406+
} catch (SQLException e) { // ✅ This CAN catch it - different try block!
407+
System.out.println("Caught from inner catch");
408+
}
409+
```
410+
411+
**💡 Learning Tips:**
412+
- **Try block rule:** "Only try block exceptions are catchable" - catch blocks can't catch exceptions from other catch blocks
413+
- **Finally guarantee:** "Finally runs then exception propagates" - finally executes but doesn't catch exceptions
414+
- **Propagation path:** "Up and out" - uncaught exceptions from catch/finally blocks propagate to calling method
415+
- **Nested solution:** "Try within try" - use nested try-catch to handle exceptions from catch blocks
416+
417+
**Common Exam Traps:**
418+
1. **Catch block exceptions** - Thinking subsequent catch blocks will handle exceptions thrown in earlier catch blocks
419+
2. **Finally block exceptions** - Assuming finally can catch its own exceptions
420+
3. **Exception propagation** - Not realizing uncaught exceptions terminate the method
421+
4. **Nested try confusion** - Not understanding when you need nested try-catch structures
422+
423+
**Q:** Can a catch block catch an exception thrown by another catch block in the same try-catch structure?
424+
**A:** No! Only exceptions thrown in the try block can be caught by catch blocks. Exceptions from catch blocks propagate up uncaught.
425+
426+
---

0 commit comments

Comments
 (0)