Junior — Middle
Is it possible to use a try-catch construct without specifying a catch block, and in which cases is this permissible?
sobes.tech AI
Answer from AI
In Java, a try block without a catch block is possible, but only if a finally block is present. That is, you can write:
try {
// code that may throw an exception
} finally {
// code that will execute in any case
}
This approach is used when you need to guarantee the execution of some code (for example, releasing resources), but do not want to handle exceptions at this point — they will be propagated further.
However, a try without catch and without finally cannot be written — the compiler will produce an error.