Is a Finally Block Without a Catch Block a Java Anti-Pattern

Is a finally block without a catch block a java anti-pattern?

In general, no, this is not an anti-pattern. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not. The whole point of exception handling is that, if you can't deal with it, you let it bubble up to someone who can, through the relatively clean out-of-band signaling exception handling provides. If you need to make sure stuff gets cleaned up if an exception is thrown, but can't properly handle the exception in the current scope, then this is exactly the correct thing to do. You just might want to be a little more careful about making sure your finally block doesn't throw.

When to use try finally and not try catch finally

I disagree, if you cannot do anything about an exception being thrown, but something further up your caller hierarchy can, then use the finally to clean up your resources and let the caller deal with cleaning up after the exception is thrown.

Using finally without a catch

Do I need the catch (Throwable t)?

No you don't.

In fact, it is harmful, because if you catch and throw Throwable like that, then for some versions of Java you will need to declare the enclosing method as throws Throwable ... and so on. (That was address in Java 8, IIRC.)

Try block without any catch statements

And where in the code would a StreamException be caught?

The try has a finally but no catch. The finally would execute, and the Exception would propagate to the caller.

Behaviour of try-catch-finally block when exceptions are thrown from both try and finally block?

Only exceptions within the try portion will be handled. Anything outside this (including the finally section isn't covered by a try and as such, Exceptions aren't handled.

If you want to catch/suppress the exception inside the finally block, you need to wrap if (br != null) br.close(); with it's own try/catch block, like this:

...
} finally {
try {
if (br != null) br.close();
} catch (Exception e) {
// whatever handling
}
}
...

Also, the exception from the try block is suppressed because that's the behavior of a try block - to try something and give you a chance to recover. Since you don't catch any exceptions after your try block, no code is run in response to it.

Then, whether or not any exception is thrown, the finally block is executed. If it throws an exception, and because it's not inside a try/catch block of its own, its Exception is propagated out the method, and to the calling method.

To take the example from your comment below, as of Java 7, you'll want to refer to the documentation outlined here, and focus on the second-to-last section entitled "Supressed Exceptions", which basically says that multiple exceptions can be thrown out of a try block, up to one exception per declared resource.

As far as what happens if the resource declaration itself throws an exception, I don't have JDK7 installed, so I'm not sure. Why not put the following code in a test project (exactly as it appears, with a bogus path), see what happens, and then tell us what the result it for everyone's benefit:

try (BufferedReader br = new BufferedReader(new FileReader("a totally invalid path"))) {
return br.readLine();
}

In a finally block, can I tell if an exception has been thrown

There is no automatic way provided by Java. You could use a boolean flag:

boolean success = false;
try {
reportStartWorkflow();
doThis();
doThat();
workHarder();
success = true;
} finally {
if (!success) System.out.println("No success");
}

No catch block still execution continues without any warning or error

  • finally block is always executed, exception or not*. That's what is causing the first printout "finally" that you see.
  • Next, the uncaught exception propagates to main, where it gets caught in the catch block, producing "exception".
  • After that, your program prints "finished" to complete the output.

That is how finally works. Moreover, if you do this

try {
throw new Exception();
} catch (Exception e) {
...
} finally {
...
}

the code in both catch and finally blocks would be executed.

* There are corner cases when you can construct a program that exits without executing a finally block, but it has nothing to do with the code in your example.

Why use finally instead of code after catch

Because if an exception gets thrown no code after the try block is executed unless the exception is caught. A finally block is always executed no matter what happens inside your try block.



Related Topics



Leave a reply



Submit