Does the finally block always execute in Java?

0 votes
I am learning Exception handling in java. I just want to know, does the finally block always get executed in Java?
May 3, 2018 in Java by Daisy
• 8,040 points
224 views

2 answers to this question.

0 votes

If you have written finally block along with try-catch block, then finally block always gets executed.

public class TryCatchFinallyDemo {

public static void main(String[] args) {

 int a=10;

 int b=0;

 int c=5;


 try {

 System.out.println(a/b); //Exception occurs

 System.out.println(a/c); //Exception does not occur


 }

 catch(Exception e)

 {

 System.out.println("It is not valid to divide any number by zero");

 }


 finally {

 System.out.println("The code ends here! Happy Learning!"); //finally block always executes

   }
 }
}

Output will be:

When exception occurs:

It is not valid to divide any number by zero
The code ends here! Happy Learning!

When exception does not occur:

2
The code ends here! Happy Learning!

The finally block does not get executed only when:

  1. If the JVm crashes
  2. if you invoke System.exit()


answered May 3, 2018 by parth295
• 4,600 points
0 votes
public static void main(String[] args) {
    System.out.println(Test.test());
}

public static int test() {
    try {
        return 0;
    }
    finally {
        System.out.println("finally trumps return.");
    }
}


Output:

finally trumps return. 
0
answered Aug 8, 2018 by Sushmita
• 6,840 points

Related Questions In Java

0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 8, 2018 in Java by geek.erkami
• 2,320 points
257 views
0 votes
1 answer

How does omitting curly braces in Java program, effect the code ?

Using braces makes the code more maintainable ...READ MORE

answered May 14, 2018 in Java by Rishabh
• 3,540 points
33 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 6,078 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
195 views
0 votes
1 answer

How do I fix a NullPointerException?

When you declare a reference variable (i.e. ...READ MORE

answered Apr 13, 2018 in Java by parth295
• 4,600 points
68 views
0 votes
1 answer

How to resolve the java.net.ConnectException in Java?

If you are getting java.net.ConnectionException, it means ...READ MORE

answered Jun 4, 2018 in Java by Akrati
• 3,150 points
119 views
0 votes
1 answer

Need for finalize() in Java

finalize() is a method called by the ...READ MORE

answered May 8, 2018 in Java by code.reaper12
• 3,450 points
63 views
0 votes
1 answer

Play MP3 files usng Java API

I don't believe the java AudioStream class ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
1,819 views
0 votes
2 answers

How does the “final” keyword in Java work?

A final variable that is created inside ...READ MORE

answered Mar 23 in Java by anonymous
28 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,600 points
42 views