Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views6 pages

Finaly Block

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

Finaly Block

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

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");
}
}

You might also like