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,140 points
2,078 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 Parth
• 4,640 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,920 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 9, 2018 in Java by geek.erkami
• 2,680 points
2,110 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,620 points
3,045 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 81,311 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,220 points
3,609 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 Parth
• 4,640 points
955 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,190 points
2,272 views
0 votes
1 answer

Need for finalize() in Java

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

answered May 9, 2018 in Java by code.reaper12
• 3,500 points
698 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,620 points
9,481 views
0 votes
3 answers

How does the “final” keyword in Java work?

Final is a keyword that is used ...READ MORE

answered Sep 1, 2019 in Java by DEEPAK KUMAR GUPTA
2,456 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 Parth
• 4,640 points
972 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP