HuntExams Academy logo
JavaBeginner#exceptions

What is the order of execution for try, catch, and finally?

Answer

try runs first; if an exception occurs, the matching catch runs. finally always executes afterward regardless of whether an exception was thrown or caught, typically used for cleanup, unless the JVM exits via System.exit().

Example

try { throw new RuntimeException("x"); }
catch (RuntimeException e) { System.out.println("caught"); }
finally { System.out.println("cleanup"); }
// prints: caught, cleanup

Related Interview Questions

1
JavaAdvanced#exceptions

What happens if an exception is thrown in a finally block?

Open
2
JavaIntermediate#exceptions

What is multi-catch in Java 7+?

Open
3
JavaIntermediate#concurrency

What is a race condition and how do you prevent it?

Open