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.