finally block (try-catch)
Hi
Although I have been programming Java for a while I have never used the finally-part of the try-catch block...really because I dont see the point. Lets make an example:
Socket s = null;
try {
s = new Socket("someserver", 80);
}
catch(Exception e) {
System.out.println("Could not connect");
}
The above is how I would usually use try-catch, and it works as intended. However, as far as I know the finally block is "whats gonna happen anyway" part of the syntax:
Socket s = null;
try {
s = new Socket("someserver", 80);
}
catch(Exception e) {
System.out.println("Could not connect");
}
finally {
s = null;
}
However, I do NOT see how this is any different from just writing the code inside the finally-statement after the try-catch block and save myself 2 lines of code:
Socket s = null;
try {
s = new Socket("someserver", 80);
}
catch(Exception e) {
System.out.println("Could not connect");
}
s = null;
Could anyone clear up the advantages of using finally instead of the method I used in the last example here?
I am aware of the fact that my example is somewhat useless in real-world situations, but I was just trying to illustrate a point.
[1337 byte] By [
invictus2a] at [2007-11-26 21:08:59]

Hi,
Finally block is used for example to make sure
objects are ready for garbace collecting and
for closing streams.
In your example, you could do this
try
{
s = new Socket(...)
// ... write to socket etc.
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
s.close();
}
kari-matti
> > The Exception may prevent the s=null from being
> > executed without the finally-block.
>
> How can that be? I thought the application continued
> execution after and exception was caught?
You are right. I misread your code. Sorry for the confusion.
> Could anyone clear up the advantages of using finally
> instead of the method I used in the last example here?
1. What does your example do if an Error is thrown?
2. What about cases where you don't want to catch the exception? Which is clearer out of Throwable th=null;
try {
...
}
catch (Throwable caught) {
th=caught;
}
...
if (th!=null) throw th;
and try {
...
}
finally {
...
}
?
3. Finally blocks also handle return statements in the try or catch blocks. (Beware: this can cause some surprises!)
int meth() throws E1, E3
try {
if doStuff() // could throw E1
return 1;
else if otherStuff() // could throw E2
return 2;
else if stillMore() // could throw E3
return 3;
else
return 4;
}
catch (E2 e2) {
// handle it. Don't just log it.
return 5;
}
finally {
something.close();
}
}
close() will always be called, even if E1, E2, or E3 is thrown, and even if no exeption is thrown and we just return
> Ok I think I perhaps get it:
>
> 1) it has support for return
> 2) if I am not catching the exceptions but are rather
> letting them get thrown past me to another external
> piece of code I can still use finally to get code run
> after the exception is thrown?
If you mean that the finally block is the last step executed before the try statement (which consists of the try block, catch blocks, and finally block) completes--regardless of how the try and catch blocks complete--then yes, you're correct.
Note that if a break or continue takes you out of a try block or catch block, finally will also be executed then.