Master java skills

Finally block

Along with try-catch, there is finally block as well. But it is optional to have it and not mandatory.

  1. Try block must be followed by either catch or finally block.
  2. Finally block always executes. It doesn’t matter whether exception occur in the try block or not.
  3. There is one scenario when finally block doesn’t execute. And that is when System.exit(1) method is called from try block.
  4. If control goes inside catch block and System.exit(1) is called from there, then also finally block will not execute.
  5. Since finally block always executes, we should keep that kinds of clean-up code, such as closing a file, a database connection or any other resource which must be closed.

Note -> There can be multiple catch blocks associated with a try block but only one finally block.

Syntax

try {}
catch (Exception e) {}
finally {
    //code
}

Example 1 – when exception doesn’t occur

In the below example, exception doesn’t occur. But as mentioned above, finally block executes successfully.

package com.javatrainingschool;

public class FinallyExample {

	public static void main (String [] args) {
		
		String name = "Arjun";
		String fullName = null;
		
		try {
			fullName = name.concat(" Kumar");
		} catch(NullPointerException npe) {
			npe.printStackTrace();
		} finally {
			System.out.println("Finally executed");
		}
		System.out.println("Full Name : " + fullName);
	}
}
Output :
Finally executed
Full Name : Arjun Kumar

Example 2 – when exception occurs

package com.javatrainingschool;

public class FinallyExample {

	public static void main (String [] args) {
		
		String name = null;
		String fullName = null;
		
		try {
			fullName = name.concat(" Kumar");
		} catch(NullPointerException npe) {
			npe.printStackTrace();
		} finally {
			System.out.println("Finally executed");
		}
		System.out.println("Full Name : " + fullName);
	}
}
Output :
java.lang.NullPointerException
	at com.javatrainingschool.FinallyExample.main(FinallyExample.java:11)
Finally executed
Full Name : null

Example 3 – when System.exit(1) is called

In this example, finally block statement will not be printed since System.exit(1) is called.

package com.javatrainingschool;

public class FinallyExample {

	public static void main (String [] args) {
		
		String name = "Arjun";
		String fullName = null;
		
		try {
			fullName = name.concat("Kumar");
			System.out.println("Going to call system.exit(1)");
			System.exit(1);
		} catch(NullPointerException npe) {
			npe.printStackTrace();
		} finally {
			System.out.println("Finally executed");
		}
		System.out.println("Full Name : " + fullName);
	}
}
Output :
Going to call system.exit(1)

What happens if we have return statement from try and finally block both

Although, it is not a good practise to return from the finally block, let’s see what happens when we have return statements from both the places i.e. try as well as finally blocks.

package com.javatrainingschool;

public class FinallyExample {

	public static void main (String [] args) {
		
		System.out.println(getFullName());
		
	}
	
	public static String getFullName() {
		
		String name = "Arjun";
		String fullName = null;
		
		try {
			fullName = name.concat("Kumar");
			return fullName;
		} catch(NullPointerException npe) {
			npe.printStackTrace();
			return fullName + "_Exception";
		} finally {
			System.out.println("Finally executed");
			return fullName + "_Finally";
		}
	}
}
Output :
Finally executed
ArjunKumar_Finally

We can see here, in such situations return value from finally block is actually returned from the method.