wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Need for finally block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need for finally block?" Watch "Need for finally block?" New topic
Author

Need for finally block?

mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi,
i have a scenario for finally block.
try{
system.out.println("try");
}catch(Exception e){
system.out.println("catch");
}finally{
system.out.println("finally");
}
the finally block gets executed irrespective of the try and catch block . Then what is the need of finally block in java.This block gets executed even when the control comes to try as well as catch block,so what is need for finally block,why not a piece of code rather than a finally block.





thanks
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi mano,

because the finally block is guaranteed to be execute regardless of whether an exception is thrown or not, it's usually a good place to free resources, close connections etc. which should be closed in both cases - when the task in the try block was successful or when an exceptions was thrown!

Marco
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
This recent thread may also be helpful.
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi,
Thanks for your reply.Yes you are true about finally block.Why i need a finally block to free my resources ill just call any method after try and catch block named test() which will do the resources cleaning.Is that fine???

thanks
Mnao
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

You can do so, but whats the bad thing in following best coding practices in Java !! If sun engineers creates finally block , we should use that , which is definitely faster than the method call .. and what if your after-the-catch-block method itself throw some exception ??, where you close resources and connection then ??


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi,

s Sagar said it's much too error-prone to invent an own concept for this kind of error handling. Things aren't alway as easy as it may seem and you're idea of an explicit method call might not always work as expected.

Anyway the exception mechanism was invented to get rid of bad error handling like returning -1 or doing strange method calls one could easily forget. Exceptions are a powerful and reliable mechanism to centralize error handling in an application.

Marco
mano ranjan
Ranch Hand

Joined: Jul 12, 2007
Posts: 102
Hi,
So as per your reply the only difference is that the finally block will be faster that other normal methods .If i use my own method instead of finally i can use try catch inside that too...

thanks
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

No mano, performance is not an important reason to prefer a finally block (if there's any difference at all)!

You have to understand the whole concept of exception handling in order to understand the advantages over a self-designed error handling method. If you're interested to learn this important topic have a look at Sun's tutorial.

Marco
Prakash Subramanian
Ranch Hand

Joined: Feb 03, 2005
Posts: 32
Keeping relevant code in the finally block is more organized and makes it easier to understand and modify. Provides a very logical way.

Any kind of return statements in your catch or try block will not stop the finally block from getting executed. Only after the finally block gets executed, does control come out of the method. It wont be so if you put code inside a custom method, as you had shown. The only exception being that of a System.exit().


Originally posted by mano ranjan:
Hi,
Thanks for your reply.Yes you are true about finally block.Why i need a finally block to free my resources ill just call any method after try and catch block named test() which will do the resources cleaning.Is that fine???

thanks
Mnao
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

This is really a beginner's question, so that's where this thread is going.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Originally posted by Rob Prime:
This is really a beginner's question, so that's where this thread is going.
I was wondering how long it would take Rob to notice

No, you don't want a test() method to do the cleanup, at least not most of the time. You do all the cleanup inside the finally{} block.
And make sure not to throw an Exception inside the finally.

Take note of what Marco told you earlier about going through the Java Tutorials about exceptions. He gave you the link.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need for finally block?
 
Similar Threads
Reg. unreachable code
Try/catch/finally
exception..try catch block execution
Can we use finaly block to catch exceptions without using catch block?