Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: finally block execution

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default finally block execution

    public class HelloWorld{
         static int a;
         public static void main(String []args){
            System.out.println("Hello World");
            HelloWorld h =new HelloWorld();
            a = h.foo();
            System.out.println(a);
         }
     
     
         int foo()
         {
         try{
         throw new Exception();
         }
         catch(Exception e){
     
         System.out.println("catch");
          return 9;
     
         }
     
     
         finally{System.out.println(a);return 10;}
    } }
    When I ran the above code output was:
    Hello World
    catch
    0
    10
    Now i have a doubt that does the return in catch is executed first or the finally block executed first??


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: finally block execution

    Hi peter_parker
    I do not understand what you are asking.
    What do you expect the output to be?

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: finally block execution

    Quote Originally Posted by jps View Post
    Hi peter_parker
    I do not understand what you are asking.
    What do you expect the output to be?
    I want to know that which block executed first catch or finally..I mean to say that after return in catch the jvm should leave the block without executing finally.Then why does value of a is 10 and not 9?

Similar Threads

  1. finally clause
    By jean28 in forum Exceptions
    Replies: 2
    Last Post: January 21st, 2013, 01:30 AM
  2. synchronized block
    By me_shankara in forum Threads
    Replies: 2
    Last Post: December 10th, 2012, 07:34 AM
  3. Finally...
    By newbie in forum The Cafe
    Replies: 2
    Last Post: September 26th, 2012, 10:14 AM
  4. Time taken for execution is more
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2011, 08:48 AM

Tags for this Thread