View Havish  Goswami 's Profile
What is the use of the finally block? Is finally block in Java guaranteed to be called? When finally block is NOT called?
Hi Java Professionals,

Please elaborate the answer
Asked by Havish Goswami | Oct 16 2017 | Report this Report abuse
Replies (7)
View Shatish  Rao 's Profile
finally block is used to perform clean-up activities or code clean-up like closing database connection & closing streams or file resources, etc

http://www.benchresources.net/finally-block-in-java-exception-handling/

http://www.benchresources.net/return-statement-with-finally-block-in-java/
Oct 20 2017 | Report this Report abuse
View Nirupama  Pathak 's Profile
finally block is used to perform clean-up activities or code clean-up like closing database connection & closing streams or file resources, etc

http://www.benchresources.net/finally-block-in-java-exception-handling/

http://www.benchresources.net/return-statement-with-finally-block-in-java/
Oct 20 2017 | Report this Report abuse
View Roshan  shah 's Profile
if system.exit() will be called in try/catch bolck i.e if jvm will get shutdown then finally block will not be called.
Oct 20 2017 | Report this Report abuse
View Chinmay  Hegde 's Profile
if system.exit() will be called in try/catch bolck i.e if jvm will get shutdown then finally block will not be called.
Oct 20 2017 | Report this Report abuse
View Nirupama  Pathak 's Profile
Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. Finally block is NOT called in following conditions

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. This may happen due to System.exit() call.
if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
If a exception is thrown in finally block and not handled then remaining code in finally block may not be executed.
Oct 19 2017 | Report this Report abuse
View Shatish  Rao 's Profile
Finally block is used if there is code that must be executed regardless of whether the exception is thrown or not.In the Finally block,clean up code related to closing of database connections,closing of files is written.

Finally block is not called in the conditions mentioned in the above post.
Oct 18 2017 | Report this Report abuse
View Tanya  Sharma 's Profile
Finally block is used if there is code that must be executed regardless of whether the exception is thrown or not.In the Finally block,clean up code related to closing of database connections,closing of files is written.

Finally block is not called in the conditions mentioned in the above post.
Oct 18 2017 | Report this Report abuse