Tuesday, November 29, 2011

Core Java Interview Questions :What is the use of finally block?

The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.:Finally block is used to handle an exception(The common error which stop the complete execution of program.) in JAVA. It can be better understood by the following code::finally block is also called as resource handler block.this block is used for frees the resources at the time of terminating the program.these resources are used for other programs.:finally block is also called as resounce handler block.it is follewed by try(if catch is there,after catch)block.it is compulsary executing block eventhough exceptions are rised.:finally block is executing by default means , whether try block rised exception or not and whether the rised exception is handiled by catch block or not by automatically finally block will be executing. :finally block is not executing in only one sinario i.e is when the try block contained the System.exit statement that time finally block will not be executing. Except this condition in all remaining conditions finally block will be executing by default.:One doubt why use Finaly ? we can give the same code to release memory and etcc... even after catch block

No comments:

Post a Comment