Loading

What is Finally Block in Java?

  • Finally is the block in Java which executes always whether the exception is handled or not therefore it contains all the necessary statements that need to be printed regardless of whether the exception occurs or not.

Flow chart of the final block:


Example:


package quipoin;
public class FinallyBlockExample {
	public static void main(String[] args) {
		try {
			System.out.println("Inside the try block!!");
			int data = 25 / 5;
			System.out.println(data); // exception will not occur
		} catch (ArithmeticException e) {
			e.printStackTrace();
		} finally {
		// whether exception occurs or not finally block will execute always
			System.out.println("Finally block is always executed!!");
		}
	}
}

Output:

Inside the try block!!
5
Finally block is always executed!!