Can a try block exist without a catch block in Java?
Yes, as long as it has either a finally block or resources in try-with-resources. A bare try with neither is a compile error.
Explain it simply
Sometimes you do not know how to handle a problem, but you still must switch off the gas stove when leaving the kitchen. A try with only finally says "I will not deal with the exception here, but this cleanup must run anyway", and the exception continues on its way to the caller.
Example
static void run() {
try {
System.out.println("working");
throw new RuntimeException("boom");
} finally {
System.out.println("cleanup"); // runs, then the exception moves up
}
}
try {
run();
} catch (RuntimeException e) {
System.out.println("caught " + e.getMessage());
}
// prints:
// working
// cleanup
// caught boomWhat to say in the interview
"Legal forms are try-catch, try-catch-finally, try-finally, and try-with-resources, which may have no catch or finally at all because the resources are closed automatically. Plain try alone does not compile. try-finally is useful when the current method cannot recover from the error but must still release something, such as unlocking a lock."
Common mistakes
- Assuming finally catches the exception. It only runs code; the exception still propagates.