What Happens if a Catch Block Throws an Exception?

 

Advanced Flow: What Happens if a Catch Block Throws an Exception?


⏱️ The 30-Second Answer

If a catch block throws a new exception that isn't handled internally, the program terminates immediately (unless caught by an outer try-catch block).

The new exception bubbles up the call stack, and any code sitting directly below that try-catch block is completely skipped.


🎯 Why Interviewers Ask This

Most junior developers assume that once execution enters a catch block, the danger zone is over.

Interviewers use this question to test your practical understanding of exception propagation. They want to see if you recognize that a catch block is just normal code—if it throws an exception, the JVM treats it with the exact same strictness as any other error.


💻 The Scenario

Consider this classic interview code snippet:


The Exact Output:

⚠️ Key Observation: The final line ("Program Ends") is never printed.


 

🧠 Step-by-Step Execution Flow

1.The Trigger:Step 1.

10 / 0 executes inside the try block. Java instantly throws an ArithmeticException and halts further execution within that block.

2.The Interception:Step 2.

The JVM locates the matching catch(ArithmeticException e) block and passes control to it. "Inside Catch" prints successfully.

3.The Mutation:Step 3.

The line throw new RuntimeException(...) executes. The original ArithmeticException is now considered fully handled and dead; a completely separate RuntimeException takes its place.

4.The Crash:Step 4.

Java scans for an outer or surrounding try-catch to handle this new exception. Finding none, it hands it to the default thread handler, printing the stack trace and killing the application.



🚗 Real-World Analogy: The Careless Mechanic

Imagine you are driving and your car gets a flat tire. That is your initial exception.


                            Fixing one problem shouldn't trigger a catastrophic secondary failure..



A roadside mechanic arrives to fix it (this is your catch block). However, while jacking up the car, the mechanic accidentally snaps the vehicle's entire axle.

The original problem (the flat tire) is technically gone, but you now have a brand new, massive failure on your hands. If there isn't another supervisor around to resolve the broken axle, your journey terminates right there on the highway.


🚫 Common Misconceptions to Avoid

  • "The original exception is attached to the new output trace automatically."

    • Correction: It is lost completely unless you practice exception chaining by passing the old exception into the new one: throw new RuntimeException("Error", e);.

  • "A finally block will be skipped if the catch block crashes."

    • Correction: The finally block always executes right before the new exception propagates out of the method.



📝 Quick Revision Cheat Sheet

SituationDoes Code Below Try-Catch Run?Does Finally Block Run?Active Exception Scent
Exception Caught safelyYesYesNone (Suppressed)
New Exception thrown in Catch❌ NoYesThe new Exception


❓ Deep-Dive Follow-Up Questions to Expect


Q1: Can we nest a try-catch block inside a catch block?

Yes. Because a catch block is ordinary Java code, you can protect against risky fallback code like this:




Q2: What happens if both the catch block AND the finally block throw exceptions?

The exception thrown by the finally block is incredibly aggressive—it will completely swallow/hide the exception thrown by the catch block. Only the finally exception propagates up to the caller.



🚀 Crack Your Next Java Interview

Reading articles is great, but consistency is what gets you hired. I drop one real, company-tested Java interview question every single day on Instagram.

No fluff—just the exact questions tech panels are asking right now, broken down simply.

👉 Follow @JavaInterviewDaily on Instagram to build your daily prep habit.




Comments

Popular posts from this blog

deployment strategy for a Spring Boot application

Why is String Immutable in Java?

Mastering the Basics: What is a Class and an Object in Java?