OneStopList

Gods and goddesses

Try Without Catch in Java: Is It Allowed?

Updated December 5, 2026

Yes, Java lets you write a try block with no catch clause, but only if it also has a finally block. The syntax try { ... } finally { ... } is valid and compiles; try { ... } on its own is a compile error. The reason is that a try statement must do something with the exceptions it can throw, and the two legal shapes are “handle them” (catch) or “guarantee cleanup” (finally).

What a try block without catch actually compiles to

When you write try with finally and no catch, the compiler still emits exception-handling bytecode. Any exception thrown inside the try body runs the finally code, then continues propagating up the call stack. Nothing is swallowed and nothing is logged unless you log it yourself. The method’s own throws clause, or a surrounding handler, is still responsible for the exception.

This is the key point people miss: try/finally does not handle anything. It only guarantees that cleanup runs. If the exception reaches main with no handler anywhere, the thread terminates and the stack trace prints, exactly as it would without the try block.

The three legal try shapes

Java accepts exactly three forms of the statement:

  • try { } catch (X e) { } — one or more catch clauses, no finally.
  • try { } finally { } — a finally clause, no catch.
  • try { } catch (X e) { } finally { } — both.

A fourth form adds a resource list: try (Resource r = ...) { }, optionally followed by catch and/or finally. The resource form is the one most people actually want when they search for a catch-free try, because it closes files, streams and connections automatically.

try-with-resources: the catch-free form you probably want

Since Java 7, a try with a parenthesised resource list needs no catch and no finally. The compiler inserts the close call for you. The resource must implement AutoCloseable (or its subinterface Closeable), which covers InputStream, OutputStream, Reader, Writer, java.sql.Connection, Statement, ResultSet, and Scanner when backed by a stream.

A worked example with a file:

try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
    String line = reader.readLine();
    System.out.println(line);
}

If readLine() throws IOException, the reader is closed before the exception leaves the block. The method still needs throws IOException or a caller that handles it. The closing happens in reverse order of declaration when you list several resources, and a failure while closing is attached to the original exception as a suppressed exception rather than replacing it.

Where the catch-free try fits in real code

Three situations call for it:

  • Cleanup that must run on every path — releasing a lock, restoring a flag, closing a channel — with the exception handled higher up.
  • Resource management where the close is the only cleanup needed.
  • Methods that deliberately declare their exceptions and let callers decide.

If none of those apply, you are probably hiding a problem. A try block that neither handles nor cleans up is dead weight.

Where people go wrong with try and finally

The most common error is writing try { ... } with nothing after the closing brace. The compiler rejects it with a message along the lines of “‘try’ without ‘catch’, ‘finally’ or resource declarations”. The fix is to add whichever of the three you actually need, not to add an empty catch just to silence the compiler.

The second mistake is an empty catch block: catch (Exception e) { }. That compiles but discards the exception, which makes failures invisible. If you truly intend to ignore an exception, name the variable and add a comment explaining why, or catch the narrowest type you can.

The third mistake is a finally block that returns or throws. A return inside finally discards any exception or return value from the try body, which silently loses errors. Keep finally to cleanup only.

Does finally always run?

Almost always. It runs whether the try body completes normally, throws, or exits via break, continue or return. It does not run if the JVM exits during the block, for example through System.exit, or if the thread is killed outright. It also does not run if the JVM crashes. For anything short of those, treat finally as guaranteed.

Choosing between catch, finally and try-with-resources

Use catch when you can do something meaningful about the failure: retry, fall back to a default, or translate the exception into one your API declares. Use finally when you must restore state that the try body may leave broken. Use try-with-resources whenever the thing you are cleaning up is an AutoCloseable, because it handles the ordering and suppressed exceptions correctly and is shorter than a manual finally.

A quick decision rule: if the cleanup is “close this object”, use the resource form. If the cleanup is “reset this field or release this lock”, use finally. If the goal is to react to the exception, use catch. If you need two of those, combine them — try with resources plus catch is legal and common.

One more check before you commit: make sure the method signature tells the truth. A catch-free try does not remove a checked exception from the method’s contract. If readLine() can throw IOException, the method must declare it or handle it somewhere. Adding finally changes when cleanup happens, not what the caller must deal with.

Related

How pages are sourced and corrected: Sources and editorial policy.