Finally block in Java is used to execute the important codes such as stream and closing connection. Even if you don’t handle the exception java finally block will always be executed. Every finally block must be followed by run or catch block.
A finally statement is always associated with try statement. Finally block identifies the statements blocks which are in need of execution irrespective of the exception occurred in try block. After the identification of try statement blocks is complete, the codes present in finally blocks are executed. During the normal process final block are always executed after the try block but during exception catch block is executed before finally block. The exception occurred in finally block is similar to the other exception. Even if the transfer elements like continue, return and break are present in try block; the code present in finally block will be executed.
Basic Syntax for the Finally block is,
try
{
// These statements that occurs an exception
}
finally
{
// These statements which are required an exception
}
What if the Statements in finally block are not executed?
There are only three cases to prevent the statements present finally block from execution.
- Thread’s death
- Usage of the function System.exit()
- Exceptions arose in finally block
- How the System.out.println() is working in Java?
Return Statement in try block:
The simple program given below can explain the scenario; execution of final block even when there is a presence of return statement in try block.
class MyMatr
{
public static void main(String args[ ])
{
System.out.println(MyMatr.TheOne());
}
public static int TheOne()
{
try {
return 108;
}
finally {
System.out.println(“My Name is Karthikh”);
System.out.println(“The finally block is working perfectly now”);
}
}
}
The output for above program will resemble:
My Name is Karthikh
The finally block is working perfectly now
108
Finally block & Close() Method:
Generally close() is used when you want to open all the streams in one go. Using close() in finally block is always regarded as a good practice. One can be sure that all output and input patterns are closed when a close(0 is used.
System.exit() and finally block:
System.exit() and return statements are entirely different. when a System.exit() statement is used in a try block; the execution of finally block is stopped.
Using finally,try and catch block:
- At Least one statement should be either associated with finally or catch block
- Finally block can perform clean-up and catch block can perform exception handling so merging finally and clean-up blocks are regarded as the best approach.
Basic Syntax for merging try, catch and finally blocks:
Try
{
//These statements that occurs an exception
}
Catch
{
// These java codes can handle the Mistakes
}
Finally
{
// These statements which are required an exception
}
Only four scenarios are available for the execution of finally block:
- When try blocks reaches the end no exception is present. Finally block will be executed after the execution of try block
- If an exception is present try block and it gets caught in the catch block. Finally block will be executed after the execution of the catch block.
- If an exception is thrown from try block and there is no matching method is present in catch block to catch the exception. In this circumstance the call to catch the method gets suspended and the try-catch blocks will now reside. Finally block will be executed before the end of the method.
- If execution of try block nears the completion, it will be to returned to the place where method was enforced. Just before the invoking of method the finally block will be executed.
The presence of return statement in try block does not matter much because the finally block will always be executed.