Finally Block: Finally block always gets executed if there is exception in program
or no exception in program.
1)If database connection is open that connection need to close so connection
closing code we will write
in finally block.
2)If file is open then file should be closed inside finally block.
syntax:
1)
try{
}catch(){
}finally{
2) try{
}finally{
# One try block can have only one finally block.
try{
}finally {
}finally{
# finally block should be at bottom.
try{
}finally{
}catch(){
Example 1: IF there is no exception program then finally block gets executed.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 2;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
} finally {
System.out.println("Finally Block");
}
System.out.println("Main method Ended");
}
}
Example 2; if there is exception in program then finally block gets executed
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
} finally {
System.out.println("Finally Block");
}
System.out.println("Main method Ended");
}
}
Example 3: If there is exception in program and we are not handling that exception
then finally
block gets executed.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (NullPointerException e) {
System.out.println("Catch Block");
} finally {
System.out.println("Finally Block");
}
System.out.println("Main method Ended");
}
}
4) If there is exception in catch block and we are not handling that exception then
finally block
will be executed but that abnormal termination.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
String s=null;
System.out.println(s.length());
} finally {
System.out.println("Finally Block");
}
System.out.println("Main method Ended");
}
}
5) If there is exception in catch block and we are handling that exception the
finally
block will be executed but that normal termination.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
String s = null;
try {
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("catch block 1");
}
} finally {
System.out.println("Finally Block");
}
System.out.println("Main method Ended");
}
}
6) If there is exception in finally block and we are not handling that exception
then it
will be abnormal termination.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
String s = null;
try {
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("catch block 1");
}
} finally {
System.out.println("Finally Block");
String s = null;
System.out.println(s.length());
}
System.out.println("Main method Ended");
}
}
Example 7: If there is exception in finally block and we are handling that
exception then it will be normal termination.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
String s = null;
try {
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("catch block 1");
}
} finally {
System.out.println("Finally Block");
String s = null;
try {
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("catch block 1");
}
}
System.out.println("Main method Ended");
}
}
Example : If System.exit(0) is called then finally block will not get executed.
package finallyBlock;
public class FinallyDemo {
public static void main(String[] args) {
System.out.println("Main method started");
int a = 20;
int b = 0;
try {
int c = a / b;
System.out.println(c);
} catch (ArithmeticException e) {
System.out.println("Catch Block");
System.exit(0);
String s = null;
try {
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("catch block 1");
}
} finally {
System.out.println("Finally Block");
String s = null;
try {
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("catch block 1");
}
}
System.out.println("Main method Ended");
}
}