Explain about Finally Block ?

#1
The Finally Block always executes when the try block exits. This ensures that the Finally Block is executed even if an Unexpected Exception occurs. But Finally is useful for more than just Exception Handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a Finally Block is always a good practice, even when no Exceptions are anticipated.

- The Runtime System always executes the statements within the Finally Block regardless of what happens within the try block. So it's the perfect place to perform cleanup. The Finally Block is a key tool for preventing resource leaks. When closing a file or otherwise recovering resources, place the code in a Finally Block to insure that resource is always recovered.

Note : If the JVM exits while the try or catch code is being executed, then the Finally Block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the Finally Block will not execute even though the application as a whole continues.
 
#2
Finally block is executed after try-catch block.

in this block we have to save any unsaved information before termination of

the program.

we can close any database connections exists.
 
#3
finally is a keyword that will used in the Exception handling concept. we have to write the finally block along with try block. The finally block get executes before the try block exists.The statements that which are put in the finally block get executed whether an exception is raised or not
Syntax:
try{
code to be monitor for errors;
}
finally{
statement1;
statement2;
}
 
#5
The at last square dependably executes when the attempt piece exits. This guarantees the at long last piece is executed regardless of the possibility that a startling special case happens. Be that as it may, at long last is valuable for something beyond special case dealing with — it enables the software engineer to abstain from having cleanup code inadvertently skirted by an arrival, proceed, or break.
 
Top