Sunday, February 26, 2017

How to use finally block in Java

In this post we will discuss and learn what is finally keyword in java and how it is useful in java,why sun people introduced this concept in java. This is one of the core concept in exception handling. Although try and catch provides terrific mechanism for trapping and handling exceptions.It is not recommended to place the clean up code inside try block because there is no guarantee for execution of all statements inside  the try block.  For example,you allocated a network socket or opened a file somewhere in the guarded region,each exception handler would have to close the file or release the socket. It makes the programmer too easy to forget to clean up the code,and also lead to a lot of redundant code. To overcome this problem java offers finally block.

finally  create a block of code that will be executed after a try/catch block. The finally block will execute whether or not an exception is thrown. Even there is a return statement in the try block, the finally block executes right after the return statement is encountered,and before the return is executes.

Therefore,finally block is the right place to close your files, release the network sockets and perform any other clean up requires. If the try block executes with no exceptions, the finally block executes immediately  after the try block completes. In case there was exception is thrown in try block,then finally block executes immediately after the proper catch block completes.

Syntax:

try{
// you need to write code in this block there is possibility of raised exceptions
}
catch(MyFirstException)

{
//put the code here that handles this exception
}
finally
{
//put the code here to release any resource  we allocated in try block

}

Example:

try{
//open database connection

//read the data
}
catch(MyFirstExcetpion e)

{
}
finally
{
//close the connection
}


Example 1:

try{
System.out.println("try");
}
catch(ArithmeticException ae)
{
System.out.println("catch");
}
finally
{
System.out.println("finally");
}

Output:
try
finally 

In the above program normal termination happens

Example 2:

try
{
System.out.println(10/0);
}
catch(ArithmeticException ae)
{
System.out.println("catch");
}
finally

{
System.out.println("finally");
}

Output:
catch
finally

In the above program also happens normal termination only

Example 3:

try
{
System.out.println(10/0);
}
catch(NullPointerException ne)
{
System.out.println("catch");
}
finally

{
System.out.println("finally");
}

Output:
finally

This program was terminated abnormally

Hence finally block should always execute irrespective of whether the execution is raised or not raised or handled or not handled.
The finally block won’t be executed if the system it self exists(JVM shutdown) i.e in the case of System.exit() finally block won’t be executed.

Recommended to Read More Topics:
MVC Architecture in Java
When to use abstract class and when interface in Java
How cookies works in servlet
IT Basics for Tech Developers


Example:
try
{
System.out.println("How are u?");
System.exit(0);
}
catch (ArithmeticException e)
{
System.out.println("catch");
}
finally
{
System.out.println("finally");

}

Output:
How are u?



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...