Exception Handling - Finally Block.

Exception Handling - Finally Block.
Finally Block

Finally block is an additional block to the try/catch block. This block is used to maintain the clean up code and the  functionality of this block is, it executes irrespective of whether there is an exception or not.

lets do write a simple java class and see how finally block behaves.

Case1: Lets have try catch finally block, but the try block doesn't raise any exception.

package com.learning.exceptionhandling;

public class SampleClass {

 public static void main(String[] args) {
  try {
   System.out.println("I am Bharani Ravi Kanth R");
  } catch (ArithmeticException e) {
   System.out.println("you are inside the catch block");
  } finally {
   System.out.println("you are inside finally block");
  }

 }

}

Lets run the code and see the output.

Output:
I am Bharani Ravi Kanth R
you are inside finally block

We can see the output, the risky code inside the try block didn't raise any exception, so catch block didn't activate, so immediately after the execution of the statements inside try block the control flows moves to finally block and started executing the statements inside the finally block.

Case2: Lets have try catch finally block, try block raises an exception of the type which catch block can handle.

package com.learning.exceptionhandling;

public class SampleClass {

 public static void main(String[] args) {
  try {
   System.out.println(100/0);
  } catch (ArithmeticException e) {
   System.out.println("you are inside the catch block");
  } finally {
   System.out.println("you are inside finally block");
  }

 }

}

Lets run the code and see the output.

Output:
you are inside the catch block
you are inside finally block


We can see the output, the risky code inside the try block raised an ArithmeticException,So catch block got activated.After the execution of catch block statements, finally block is also executed.

Case3:  Lets have try catch finally block, try block raises an exception which is not of type, catch block can handle

package com.learning.exceptionhandling;

public class SampleClass {

 public static void main(String[] args) {
  try {
   String[] stringArray = null;
   System.out.println(stringArray[0]);
  } catch (ArithmeticException e) {
   System.out.println("you are inside the catch block");
  } finally {
   System.out.println("you are inside finally block");
  }

 }

}

Lets run the code and see the output.

Output:
Exception in thread "main" java.lang.NullPointerException
 at com.learning.exceptionhandling.SampleClass.main(SampleClass.java:8)
you are inside finally block

We can see now, catch block couldn't handle the exception, so there was abnormal termination of the code, but before terminating finally block was executed.

We can conclude from case1, case and case3 that irrespective of no exceptions or handling the exceptions or not finally block will be always executed.

Finally block is majorly used when our program majorly deals with network. Best real time example we can give is When we are doing CRUD operations through database we have the close connections logic inside the finally block.

Rules of using Finally Block:
a. finally block can be used only if there is a try block. below are the valid syntax

Valid syntax 1:

try{
//risky code
}
catch(type of exception object){
}
finally{
}

Valid syntax 2: we can have try block with finally without catch.
try{
//risky code
}
finally{
}
Invalid case 1: there should be no statements between try catch and finally blocks, this will through compile time error.

try{
//risky code
}
catch(type of exception object){
}
System.out.println("statement 1")
finally{
}
Invalid case 2: there should be no statements between try  finally blocks, this will through compile time error.
try{
//risky code
}
System.out.println("statement 1")
finally{
}

b. Each try block can have only one finally block. i.e, below use of finally block is invalid

Invalid Syntax: 

try{
}
catch(type of exception object){
}
finally{
}
finally{
}

Invalid Syntax: 

try{
}
finally{}
finally{
}

if you more than one finally block you will get compile time error.