DEV Community

Sharath Kumar
Sharath Kumar

Posted on

What Happens If Exception Occurs in Finally Block?

Introduction

In real-world Java applications, exception handling is critical for building robust systems. However, many developers misunderstand what happens when an exception occurs inside the finally block. This confusion can lead to lost exceptions, hidden bugs, and production issues.

πŸ‘‰ Direct Answer: If an exception occurs in the finally block, it overrides any exception thrown in the try or catch block, and only the finally exception is propagated.


Understanding try-catch-finally Flow

Before diving deeper, let’s understand the execution flow:

  • try block executes first
  • If an exception occurs β†’ catch executes
  • finally block executes always (except rare JVM shutdown cases)

Case 1: Exception Only in Finally Block

public class Test1 {
    public static void main(String[] args) {
        try {
            System.out.println("Inside try block");
        } finally {
            int result = 10 / 0; // ArithmeticException
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Expert Annotation

  • try executes normally
  • finally executes and throws exception
  • Program terminates with ArithmeticException

Edge Case

  • Even if try is successful, exception in finally will crash the program

Case 2: Exception in Both Try and Finally

public class Test2 {
    public static void main(String[] args) {
        try {
            int a = 10 / 0; // ArithmeticException
        } finally {
            String str = null;
            str.length(); // NullPointerException
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Expert Annotation

  • try throws β†’ ArithmeticException
  • finally throws β†’ NullPointerException
  • Output β†’ Only NullPointerException

Edge Case

  • Original exception is completely lost
  • Debugging becomes extremely difficult

Case 3: Exception in Catch and Finally

public class Test3 {
    public static void main(String[] args) {
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            throw new RuntimeException("Exception in catch");
        } finally {
            throw new RuntimeException("Exception in finally");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Expert Annotation

  • try throws exception
  • catch handles and throws new exception
  • finally throws another exception
  • Final Output β†’ Exception from finally

Edge Case

  • Both try and catch exceptions are ignored

Case 4: Return Statement with Finally Exception

public class Test4 {
    public static int testMethod() {
        try {
            return 10;
        } finally {
            throw new RuntimeException("Exception in finally");
        }
    }

    public static void main(String[] args) {
        System.out.println(testMethod());
    }
}
Enter fullscreen mode Exit fullscreen mode

Expert Annotation

  • try attempts to return 10
  • finally executes before return
  • Exception in finally overrides return

Edge Case

  • Return value is never returned
  • Exception dominates execution

Case 5: Safe Handling Inside Finally

public class Test5 {
    public static void main(String[] args) {
        try {
            int a = 10 / 0;
        } finally {
            try {
                String s = null;
                s.length();
            } catch (Exception e) {
                System.out.println("Handled exception in finally");
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Expert Annotation

  • try throws exception
  • finally handles its own exception
  • Original exception is preserved

βœ” Edge Case

  • Proper handling prevents loss of root cause

Why This Behavior is Dangerous

In my decade of teaching Java, I’ve seen many production issues due to this:

  • Root cause of failure is lost
  • Logs become misleading
  • Debugging takes more time

Our students in Hyderabad often face this in real-time projects, especially while handling database connections or file streams.


Best Practices to Follow

Always Follow These Rules:

  • Never throw exceptions directly in finally
  • Always wrap risky code inside finally with try-catch
  • Prefer try-with-resources

Try-With-Resources (Modern Solution)

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    System.out.println(br.readLine());
}
Enter fullscreen mode Exit fullscreen mode

βœ” Benefits:

  • Automatically closes resources
  • Preserves original exception
  • Adds suppressed exceptions

Comparison Table

Scenario Result Risk Level
Exception only in try Propagated normally Low
Exception only in finally Finally exception propagated Medium
Exception in try & finally Finally overrides try High
Exception in catch & finally Finally overrides catch High
try-with-resources Original + suppressed exceptions kept Safe

Real-Time Use Cases

This concept is critical in:

  • Database connection handling
  • File I/O operations
  • Logging systems
  • Transaction management

Common Mistakes Developers Make

  • Writing cleanup logic without exception handling
  • Ignoring suppressed exceptions
  • Misusing return statements with finally

When Finally May Not Execute

Although rare, finally may not execute if:

  • System.exit() is called
  • JVM crashes
  • Power failure

Learn More (Recommended)

If you want to deeply understand exception handling, multithreading, and real-time Java scenarios, I highly recommend:
πŸ‘‰ https://www.ashokit.in/courses/core-java-online-training


Advanced Insights (From Experience)

In enterprise systems:

  • Exception handling is part of system design
  • Losing exceptions can break monitoring systems
  • Logging frameworks rely on correct exception flow

πŸ‘‰ Always think beyond syntaxβ€”focus on behavior and impact


Quick FAQ

1. What happens if finally throws exception?

It overrides all previous exceptions.

2. Does finally always execute?

Yes, except in JVM shutdown scenarios.

3. Can finally override return statement?

Yes, it executes before return and can override it.

4. What is safer alternative?

try-with-resources.

5. How to avoid losing exceptions?

Handle exceptions inside finally block.


Final Thoughts

Understanding what happens when an exception occurs in the finally block is crucial for writing reliable Java applications.

Small mistakes here can lead to major production issues, so always follow best practices and modern approaches.

If you're serious about mastering such advanced Java concepts, explore:
πŸ‘‰ https://www.ashokit.in/courses/core-java-online-training

It’s one of the Best AI powered Core JAVA Online Training in Hyderabad, designed to help you crack real-time challenges and interviews with confidence.

Top comments (0)