Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need for finally block?

 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This recent thread may also be helpful.
 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really a beginner's question, so that's where this thread is going.
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic