1. It is not recommended to place cleanup code inside try statement because there is no guarantee for execution of all statements inside try block.
  2. It is not recommended to maintain cleanup code in catch block because if there is no exception then catch blocks won’t be executed.
  3. We required a block to maintain cleanup code which should execute always irrespective of whether the exception is raised or not , such block is nothing but “finally block“.Hence the main objective of finally block is to maintain cleanup code.

try{
//open the database connection
//read the data
}catch(Exception e){

}finally{
//close the Connection
}

Example:1Without Exception

public class ExceptionDemo {

	public static void main(String[] args) {
		try{
			System.out.println("try");
		}catch(Exception exception) {
			System.out.println("catch block");
		}finally{
			System.out.println("finally block");
		}
	}
}
/*
Output:
try
finally block
*/

Example:2With Exception

public class ExceptionDemo {

	public static void main(String[] args) {
		try {
			System.out.println(10/0);
		} catch (ArithmeticException exception) {
			System.out.println("catch block");
		} finally {
			System.out.println("finally block");
		}
	}
}
/*
Output:
catch block
finally block
*/

The finally block won’t be executed if the system itself exists(JVM shutdown) i.e in the case of System.exit().

Example:1Using System.exit(0) statement

public class ExceptionDemo {

	public static void main(String[] args) {
		try {
			System.out.println("try");
			System.exit(0);
		} catch (ArithmeticException exception) {
			System.out.println("catch");
		} finally {
			System.out.println("finally");
		}
	}
}
/*
Output:
try
*/

Example:2Using return statement

public class ExceptionDemo {

	public static void main(String[] args) {
		try {
			System.out.println("try");
			return;
		} catch (ArithmeticException exception) {
			System.out.println("catch");
		} finally {
			System.out.println("finally");
		}
	}
}
/*
Output:
try
finally
*/

Difference Between final, finally, finalize

Modifier/Class/Block Description
final It is the modifier applicable for variables,methods and classes.

  1. If a variable is declared as final,Then variable will become a constant and hence re-assignment is not possible.
  2. If a method is declared as final, then overriding is not possible.
  3. If a class is declared as final,then child class creation is not possible i,e inheritance is not possible.
finalize It is a method available in Object class.To perform cleanup activities Garbage Collector always calls this method just before destroying any object.
finally It is a block always associated with try catch.to maintain cleanup code this method should be always be executed.

Note: when compared with finalize() method, finally is always recommended to maintain cleanup code because there is no guarantee for the exact behavior of “Garbage Collector” ,it is Virtual Machine Dependent.

Possible combinations of try, catch, finally

trycatchfinally