• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Real use of finally block

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic