Exceptions: an exception thrown from finally replaces the exception thrown from try

When both the try block and its finally block throw, the finally block's exception is the one that actually propagates to the caller; the original exception from try is discarded unless it was explicitly attached first.

Code
try {
    try {
        throw new RuntimeException("from try");
    } finally {
        throw new IllegalStateException("from finally");
    }
} catch (Exception e) {
    System.out.println("Propagated: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
Output
Propagated: IllegalStateException: from finally
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms