As the
finally
block will always be executed, not depending upon the fact whether you are getting any exceptions or not and how you are handling those caught exceptions, critical codes which are to be always executed, are written within finally
block. For example, closing database connections or closing an already open file is always critical for any application-level program, hence just to ensure, that these are being executed, we generally put the codes for database connection closing or file stream closing, into finally
block.Example of Using finally Block
In the following example, the statement inside the try block would never cause any exception, so the control will never get into the catch block. But it will get inside the finally block.
public class DemoFinally {
public static void main(String[] args) {
int i = 25;
int j = 5;
int k = 0;
try {
k = i/j;
}catch(ArithmeticException e) {
e.printStackTrace();
}finally {
System.out.println("No Excerption...Still finally block is executed");
}
}
}
In the following example, the statement inside the try block would certainly cause an Arithmetic Exception due to the divide-by-zero situation. So it will get into the catch block and will print the stack trace. After that, the control of the program will get inside finally block and the message would be printed at the console following the printed stack trace.
public class DemoFinally {
public static void main(String[] args) {
int i = 25;
int j = 0;
int k = 0;
try {
k = i/j;
}catch(ArithmeticException e) {
e.printStackTrace(); // if this exception has not been handled like this, still finally block would have been reached.
}finally {
System.out.println("Excerption occurs...Still finally block is executed");
}
}
}
The above two examples illustrate that it does not matter whether exceptions are there, whether you are handling it or not, the finally block will always be executed.
Writing Finally block only after Try, excluding Catch!
Yes, that is possible. You can omit the catch block, if and only if you insert a finally block after a try block, like the following.
public class DemoFinally {
public static void main(String[] args) {
int i = 25;
int j = 5;
int k = 0;
try {
k = i/j;
}
finally {
System.out.println("Excerption occurs...Still finally block is executed");
}
}
}
In the above example as there is no chance of encountering an Arithmetic Exception, finally block would be executed just after the code within the try block gets executed. But what if the value of
j
in the above code is 0? Then, it is needless to say that an Arithmetic Exception will occur, but as there is no one to catch this exception, a stack trace for this exception would be printed just after the execution of finally block.What happens to finally block if it is after an exit or return statement?
If we encounter
System.exit(0)
before finally
, then codes within finally
block would never been reached. For example, in the following code, codes in finally
block will never be reached as the control of the program will hit System.exit(0)
in any case either from try or catch block.
import java.util.Scanner;
public class DemoFinally {
public static void main(String[] args) {
int i = 25;
int k = 0;
Scanner sc = new Scanner(System.in);
int j = sc.nextInt();
try {
k = i/j;
System.out.println("Value of k = " + k);
System.exit(0);
}
catch(ArithmeticException e)
{
e.printStackTrace();
System.exit(0);
}
finally {
System.out.println("This line of code will never work..");
}
}
}
On the other hand, the control of the program will always reach
Let us check out the following code
finally
block even if it encounters return
statement anywhere before the finally
block.
import java.util.Scanner;
public class DemoFinally {
public static void main(String[] args) {
int i = 25;
int k = 0;
Scanner sc = new Scanner(System.in);
int j = sc.nextInt();
try {
k = i/j;
System.out.println("Value of k = " + k);
return;
}
catch(ArithmeticException e)
{
e.printStackTrace();
System.exit(0);
}
finally {
System.out.println("Hello World");
}
}
}
In the above program, codes within the finally
block is always reachable, because depending on the value of variable j
, code either branches out to catch block or stays within try. But in any case, it will encounter a return
statement. But even if, we are encountering return
, the finally
block will always be reachable, hence we will get Hello World
in every case.
Hope, you enjoyed this.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.