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:
CODE without FinallyBlock:
I'm waiting for your helps, friends. Thanks
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.
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:
CODE without FinallyBlock:
I'm waiting for your helps, friends. Thanks
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"); }
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.
Yes finally block will always be executed except when you have system.exit() in try or catch ..
Regards Qazi
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.
A finally block contains all the crucial statements that must be executed whether exception occurs or not. The statements present in this block will always execute regardless of whether exception occurs in try block or not such as closing a connection, stream etc.