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 thecatch
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
Asynchronous Controller Is Blocking Requests in ASP.NET MVC Through Jquery
JavaScript Serialization of Datetime in ASP.NET Is Not Giving a JavaScript Date Object
How to Get Utc Offset in JavaScript (Analog of Timezoneinfo.Getutcoffset in C#)
How to Parse an HTML String for Image Tags to Get at The Src Information
How to Install Python for .Net on Windows
Passing Eval from Aspx to JavaScript Function as Parameter
Error Handling When Downloading File from ASP.NET Web Handler (.Ashx)
How to Display a List of Images, from a Folder on Hard Drive, on ASP.NET Website
C# Datetime.Ticks Equivalent in Java
How to Develop iOS App Using Xamarin Studio on Windows
Method Overloading. Can You Overuse It
Dynamic Form Generation in Asp.Net
How to Get The Http Post Data in C#
Getting a Request.Headers Value