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 7 of 7

Thread: Finally Block

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

    Default Finally Block

    What is the purpose of using Finally Block?
    I write code without Finally Block and with Finally Block. The result is same.

    CODE with FinallyBlock:
    public class Exception{
    public static void main(String[] args){
    int x = 4, y = 0;
    try{
    System.out.println(x/y);
    } catch(ArithmeticException e){
    System.out.println(e.toString());
    }
    finally{
    System.out.println("CODE with FinallyBlock");
    }
    }
    }
    CODE without FinallyBlock:
    public class Exception{
    public static void main(String[] args){
    int x = 4, y = 0;
    try{
    System.out.println(x/y);
    } catch(ArithmeticException e){
    System.out.println(e.toString());
    }
     
    System.out.println("CODE without FinallyBlock");
     
    }
    }
    I'm waiting for your helps, friends. Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Finally Block

    Don't post the same question multiple times on this forum, once is enough. Also, please use code tags.

    Finally tags are always executed, regardless of if an exception was caught/thrown/propagated. In your above example there's no difference because the second example is guaranteed to always be executed (because you are catching the only possible exception that could be thrown).

    However, consider a case where multiple exceptions could be thrown, or if your try block throws an exception:

    public static void ExceptionGenerator(int i) throws ArithmeticException, IOException
    {
    	if (i % 2 == 0)
    	{
    		throw new ArithmeticException();
    	}
    	else
    	{
    		throw new IOException();
    	}
    }
     
    public static void main(String[] args)
    {
    	try
    	{
    		ExceptionGenerator(0);
    	}
    	catch (IOException e)
    	{
    		System.out.println("IOException caught");
    		throw new ArithmeticException();
    	}
    	finally
    	{
    		System.out.println("Guaranteed to execute");
    	}
    	System.out.println("This won't be executed if some exception other than an IOException was caught, or if an exception was thrown in the catch block");
    }

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

    Default Re: Finally Block

    Dear friend, Finally block is always executed. In the above example (1st) if you do not use catch block then the exception is occur and this exception is handled by JVM, and then JVM terminate the program, but before terminate the program the finally block must be executed.
    And in the above example(2nd), If you do not use catch block then the exception is occur and your program will be terminated by the JVM. It means any statements are not be executed.

  4. #4
    Junior Member qazi's Avatar
    Join Date
    Jul 2013
    Location
    Peshawar, Pakistan
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finally Block

    Yes finally block will always be executed except when you have system.exit() in try or catch ..
    Regards Qazi

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finally Block

    it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

  6. #6
    Junior Member
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finally Block

    The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

    Lavanya.J
    Indium Software India Limited.
    ....

  7. #7
    Member Paramvir's Avatar
    Join Date
    Dec 2013
    Location
    Surrey, British Columbia, Canada
    Posts
    41
    My Mood
    Nerdy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Finally Block

    Quote Originally Posted by helloworld922 View Post
    Don't post the same question multiple times on this forum, once is enough. Also, please use code tags.

    Finally tags are always executed, regardless of if an exception was caught/thrown/propagated. In your above example there's no difference because the second example is guaranteed to always be executed (because you are catching the only possible exception that could be thrown).

    However, consider a case where multiple exceptions could be thrown, or if your try block throws an exception:

    public static void ExceptionGenerator(int i) throws ArithmeticException, IOException
    {
    	if (i % 2 == 0)
    	{
    		throw new ArithmeticException();
    	}
    	else
    	{
    		throw new IOException();
    	}
    }
     
    public static void main(String[] args)
    {
    	try
    	{
    		ExceptionGenerator(0);
    	}
    	catch (IOException e)
    	{
    		System.out.println("IOException caught");
    		throw new ArithmeticException();
    	}
    	finally
    	{
    		System.out.println("Guaranteed to execute");
    	}
    	System.out.println("This won't be executed if some exception other than an IOException was caught, or if an exception was thrown in the catch block");
    }
    This.

    However a finally isn't "guaranteed" to be executed. An example would be if the JVM terminated.
    If you'd like to get ahold of me, email me: paramvirphagura339@gmail.com


Similar Threads

  1. finally block execution
    By peter_parker in forum Java Theory & Questions
    Replies: 2
    Last Post: April 1st, 2013, 01:54 AM
  2. finally clause
    By jean28 in forum Exceptions
    Replies: 2
    Last Post: January 21st, 2013, 02:30 AM
  3. Finally...
    By newbie in forum The Cafe
    Replies: 2
    Last Post: September 26th, 2012, 11:14 AM