• 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

finally{} ?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


finally is always executable block right? so,inside finally block ,we put DataBase connection closing stuff(con.close()..etc..) ...

my question is whether finally is used only for This pupose?..

please anyone can explain me...

Thanks & Regards,
seetharaman.v
[ May 31, 2008: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by seetharaman venkatasamy:
finally is always executable block right?


Correct. It gets executed regardless if an an exception is thrown in the corresponding try block. One thing to note is that if an exception is thrown and not handled within a finally block itself, code after that exception does not get executed. For example, in the following code:


if the doSomeCleanup() method causes an exception (say a NullPointerException), the close() method never gets called. This can sometimes lead to hard to track down bugs.

Originally posted by seetharaman venkatasamy:
so,inside finally block ,we put DataBase connection closing stuff(con.close()..etc..) ...


Correct.

Originally posted by seetharaman venkatasamy:

my question is whether finally is used only for This purpose?


Generally yes. A finally block's primary purpose is to prevent resource leaks or issues by doing appropriate cleanup. At a higher level, it is used to ensure a piece of code is executed, even if an exception occurs in the corresponding try block.

Take a look at the Java Tutorial page on The finally Block for some more information.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify: When I said the code in a finally block is executed even if an exception occurs in the corresponding try block, that means that the code in a finally block gets executed any time the try block is exited, including if an exception occurs. So if the try block is exited due to a return statement, program flow, a break statement, or an exception, the code in the finally block is executed. I just wanted to make sure that was clear since I may not have worded it the best in my initial reply.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two (that I know of) exceptions that will cause a finally block not to be executed:
- when System.exit is called
- when the JVM crashes, e.g. because of a bug in some native code

Of course, in both cases there is no more JVM active to execute the block, so it's quite logical.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot mark

regards,
seetharaman.v
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic