Use of finally block in java with some important statements

In this post, we will see the use of finally block in java with a different system and see the different behavior with each statement.

finally block in java

Here is the table content of the article will we will cover this topic.
1. finally block with a return statement
2. finally block with System.exit()
3. Exception in the finally block

1. finally block with a return statement

As we know finally block, always execute whether an exception occurs or not. If you are using a return statement in, try or catch block then finally block override the return of try/catch block. It means when the compiler encounters the return statement in try/catch block it directly jumps to finally block and execute the statements of finally block.

Let’s try to explain with an example, In this example, we are printing calculated data which is return by a method calculatedData() and its return type is String. See the Use of finally block in java

public class UseOfReturnWithFinallyBlock
{  
  public static void main(String args[])
  {  
  	  System.out.println(calculatedData());
  }  
 
  public static String calculatedData()
  {
	  String calculatedData = null;
	try
	{
		calculatedData = "This data is return from try block";
		return calculatedData;
	}
	catch(ArithmeticException e)
	{
		e.printStackTrace();
		calculatedData = "This data is return from catch block";
		return calculatedData;
	}
	finally
	{
		calculatedData = "This data is return from finally block";
		return calculatedData;
	}
	  
  }
}

Output: This data is return from finally block

Explanation: There is no exception raised in a try block, so it is executed successfully. During the execution of try block when compiler encounters the return statement in try block it directly jumps to finally block and execute that block. It will override the return statement of the try block.

2. finally block with System.exit()

As you know exit() is a method used to terminates the current Java virtual machine running on the system. If you are using this method in your program, then the compiler will not execute the finally block.

public class UseOfExitMethodWithFinallyBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		System.out.println("Rest code inside the try block"); 
		System.exit(0); 
  	}
	catch(Exception e)
	{
		e.printStackTrace();
                System.out.println("If exception raised, catch block will execute");
	}	
  	finally
	{
		System.out.println("This is finally block"); 
	}
	   //rest code of the program   
  	 System.out.println("This code is outside from the try block");  
  }  
}

Output: This code is inside in the try block
Rest code inside the try block

3. Exception in the finally block

finally block will not be executed if any exception raised in finally. Because exception behaves the same for all blocks.

public class Exception in ExceptionRaisedInFinallyBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		System.out.println("Rest code inside the try block"); 
		
  	}
	catch(Exception e)
	{
		e.printStackTrace();
System.out.println("If any exception raised in try block then catch block will execute");
	}	
  	finally
	{
		int a = 2/0;
		System.out.println("This is finally block"); 
	}
	   //rest code of the program   
  	 System.out.println("This code is outside from the try block");  
  }  
} 

Output: This code is inside in the try block
Rest code inside the try block
Exception in thread “main” java.lang.ArithmeticException: / by zero       at ExceptionRaisedInFinallyBlock.main(ExceptionRaisedInFinallyBlock.java:18)

Leave a Comment