Wednesday, 1 February 2017

FINALLY BLOCK IN JAVA

Finally block is the block which always executes even the exception is handled by catch block or not.

Example –
import java.util.Scanner;
public class Exception1 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   System.out.println("Enter number1 : ");
                   int num1 = sc.nextInt();
                   System.out.println("Enter number2 : ");
                   int num2 = sc.nextInt();
                   try
                   {
                             int k = num1/num2;
                             System.out.println(k);
                   }
                   catch(ArrayIndexOutOfBoundsException e)
                   {
                             System.out.println("Array index out of bound");
                   }
                   finally{
                             System.out.println("Finally block is executed");
                             sc.close();
                   }
          }
}

Output –
5
Enter number2 :
0
Finally block is executed
Exception in thread "main" java.lang.ArithmeticException: / by zero
          at excep.Exception1.main(Exception1.java:14)

Note – Even the ArithmeticException is not handled by catch block but it still executes the finally block.

Example –
import java.util.Scanner;
public class Exception1 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   System.out.println("Enter number1 : ");
                   int num1 = sc.nextInt();
                   System.out.println("Enter number2 : ");
                   int num2 = sc.nextInt();
                   try
                   {
                             int k = num1/num2;
                             System.out.println("The result is : "+k);
                   }
                   catch(ArithmeticException e)
                   {
                             System.out.println("Number2 cannot be zero");
                   }
                   finally{
                             System.out.println("Finally block is executed");
                             sc.close();
                   }
          }
}

Output –
Enter number1 :
10
Enter number2 :
2
The result is : 5
Finally block is executed


As per coding standard in finally block we should write resource releasing logic or clean up code. Resource releasing logic means unreferencing objects that are created in try block. For example – in realtime projects we create JDBC API objects(Connection, Statement etc) in try block and at the end of the try we must close those objects, since the statements written in try and catch are not guaranteed to be executed so we must place them in finally block.


Inner finally block – We can also write finally for inner try block, that finally block is called inner finally.

Example –
import java.util.Scanner;
public class Exception1 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   System.out.println("Enter number : ");
                   int num = sc.nextInt();
                   int a[] = new int[num];
                   try
                   {
                             System.out.println("The value of num is : "+num);
                             try
                             {
                                      for(int i=0;i<=num;i++)
                                      {
                                                System.out.println(a[i]=i);
                                      }
                             }
                             catch(NullPointerException e)
                             {
                                      System.out.println("NPE exception");
                             }
                             finally{
                                      System.out.println("I am a inner finally");
                             }
                   }
                   catch(ArrayIndexOutOfBoundsException e)
                   {
                             System.out.println("Outer catch handles the inner try exception");
                   }
                   finally{
                             System.out.println("I am a outer finally");
                   }
          }
}

Output –
Enter number :
5
The value of num is : 5
0
1
2
3
4
I am a inner finally
Outer catch handles the inner try exception
I am a outer finally 

No comments:

Post a Comment