Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Real use of finally block

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the real use of finally block in try-catch-finally?
(Anything inside finally executes anyway..right?!)
If an exception is caught in a catch block,anyway the contents/lines below the block is going
to execute..Then what is the specific need of finally block?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well what if you are rethrowing the exception that you have caught then i don't think any line below the catch block will run. But finally will always run even if you are rethrowing the exception that has been caught.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There doesn't even have to be a catch block, you can just have try-finally:
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah try alone cannot exist. try should be paired either with catch or finally or both in the order try catch finally.
 
Jaydeep Mazumdar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the case when your code throws some un-anticipated exceptions (e.g unchecked exceptions like NullPointerException) which are not explictly caught in the catch block.

In such a case, since the exception is not caught, the program will dump the trace and exit. However, if you have some cleanup to be done, you can do it in the finally block.

Infact, you can also have only try and finally blocks. e.g.

 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Yeh the finally block would usually be used some cleanup code.

For example if you are reading from a database or from a file you would want to ensure that the connections to the file/database are closed.
You would want to do this if the application completes normally or if there was an exception thrown... so in this case you would use the finally block.

Sean
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yeah closing a particular application is a good clean up but we have to see the closing method does not throw a checked exception which will land another try catch in your finally block. This happens in the file operations when you try to put the close method in the finally block without handling or declaring the checked exception. So we need to keep special watch on the clean up codes that we put in the finally block.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all...thanks for all your nice explanations..!
so is it ok if we put another try-catch-finally inside finally....because connection.close() - clean up requires exception to be caught or thrown..
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not try it out writing a program because you will remember that. I guess you will find the answer there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic