finally block in java

We have learned about try block and catch block in java. In this topic, we will read what is finally block in java and how to use it. Let’s discuss the try catch finally java in example

Here is the table content of the article will we will cover this topic.
1. What is finally block?
2. finally block without catch block?
3. finally block with catch block?
4. Important points of finally block?

5. finally, block with a return statement?
6. finally block with System.exit()?
7. Exception in the finally block?

What is finally block in java?

finally, block executes after the execution of the try block. A finally block contains all statements that must be executed whether an exception occurs or not. If you want to perform any necessary operation in your program whether an exception occurs or not, then you can use finally block.

There are two ways to use finally block:

finally block without a catch block

You can use finally block with a try block. It’s not mandatory to use a catch block with a try. Catch and finally block are optional but you can’t skip the both at the same time. So, if you are using try block then you should use either catch or finally block. You cannot use finally without a try block.

try 
{
    // code that might throw an exception
} 
finally 
{
   //Statements to be executed
}

There are two cases:

i. If no exception raised in the try block, try block executes and control will be transferred to finally block, then the rest of the code will be executed.
ii. If any exception raised in the try block, then the rest of the try block doesn’t execute and control will be transferred to finally block, and the rest of the code will not be executed.

finally block in java

The first case when no exception raised:

Let discuss this scenario with an example. In this example, we are printing some strings. We are using try block with finally block and skipping the use of catch block.

public class ExampleFinallyWithoutCatchBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		String s = "Ravikant";
		System.out.println("result of calculation = "+ s);
  		System.out.println("Rest code inside the try block");
  	}
	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
result of calculation = Ravikant
Rest code inside the try block
This is finally block
This code is outside from the try block

Explanation of example: There is no exception raised in try block. So, try block executes successfully and after that finally and rest of code executes.

Second case when exception raised:

In this example, we are printing some strings. We are using try block with finally block and skipping the use of catch block.

public class ExampleFinallyWithoutCatchBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		String s = null;
		System.out.println("result of calculation = "+ s.toString()); 
		System.out.println("Rest code inside the try block"); 
  	}
	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
This is finally block
Exception in thread “main” java.lang.NullPointerException at ExampleFinallyWithoutCatchBlock.main(ExampleFinallyWithoutCatchBlock.java:9)

Explanation of example: In the above example exception raised in the try block. After the occurrence of exception try block doesn’t execute and control directly goes to finally block. Rest of the code will not be executed.

2. finally block with a catch block

You can use finally block with try and catch block. You must place finally block after the catch block. Because you can’t place any statement/block between try and catch block.

try 
{
    // code that might throw an exception
} 
Catch
{
	// Exception handling
}
finally {
   //Statements to be executed
}

There are two cases:

i. If no exception raised in a try block, try block executes successfully and catch block will be skipped automatically because no one exception occurred in the try block. And control will be transferred to finally block, then the rest of the code will be executed.
ii. If any exception raised in the try block, then the rest of the try block doesn’t execute and controls go to catch block as exception handled by the catch block. After that control will be transferred to finally block, and the rest of the code will execute.

finally block in java

The first case when no exception raised:

Let discuss this scenario when no exception raised. In this example, we are printing some strings. We are using finally block with try and catch block.

public class ExampleFinallyWithCatchBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		String s = "Ravikant";
		System.out.println("result of calculation = "+ s);
  		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
	{
		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
result of calculation = Ravikant
Rest code inside the try block
This is finally block
This code is outside from the try block

Explanation of example: There is no exception raised in try block. So, try block executes successfully. The catch block will be skipped automatically. After execution of try block  control directly jump to finally block and rest of code executes.

Second case when exception raised:

In this example, we are printing some strings. We are using finally block with try and catch block.

public class ExampleFinallyWithCatchBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		String s = null;
		System.out.println("result of calculation = "+ s.toString()); 
		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
	{
		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
java.lang.NullPointerException at ExampleFinallyWithCatchBlock.main(ExampleFinallyWithCatchBlock.java:9)
If any exception raised in try block then catch block will execute This is finally block This code is outside from the try block

Explanation of example: In above example exception raised in try block. After the occurrence of exception try block doesn’t execute and control directly goes catch block. catch block handling the exception and print some line of code. After handling the exception now finally block will be execute and then rest of code will be execute.

Important points of finally block

  • finally block are optional, you can handle any exception by using try and catch block.
  • You can’t use finally block without a try block. You should place the statements in this block that must be executed either exception occurs or not.
  • If any exception occurs in finally block, then you need to handle it.

Leave a Comment