Skip to content

Instantly share code, notes, and snippets.

@serac
Last active December 16, 2015 10:29
Show Gist options
  • Save serac/5420655 to your computer and use it in GitHub Desktop.
Save serac/5420655 to your computer and use it in GitHub Desktop.
Demonstrate that catching an exception in a finally block does not consume the exception thrown from the outer try block.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class FinallyThrowsTest {
public static void main(final String[] args) {
try {
throw new RuntimeException("Thrown from outer try");
} finally {
try {
throw new RuntimeException("Thrown from try/catch inside finally.");
} catch (Exception e) {
System.out.println("Caught an exception in finally: " + e.getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment