File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why do v use finally block in exception handling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do v use finally block in exception handling?" Watch "Why do v use finally block in exception handling?" New topic
Author

Why do v use finally block in exception handling?

smyle khanna
Ranch Hand

Joined: Jan 14, 2004
Posts: 54
Hi..

I want 2 know y do v use finally block in exception handling when the statements after the try catch block can b executed without a finally block n irrespective of a exception.
thanks
Smyle
vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
Hi,

A finally clause typically contains code to release resources acquired in its corresponding try block,this is an effective way to eliminate leaks.

Java gurantees that finally clause (if one is present following a try/catch sequence) will execute whether or not an exception is thrown in the corresponding try block or any of its corresponding catch clauses.

Hope this helps,
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
Hello my brother! I learned this today too! Analyze these two:

Without finally:


With finally:


Now as you can see, we've lost, ONE line of code! I know, I know, thank you Java & C# for throwing in finally as a keyword, you have saved us. Not only finally! But goto as well!
[ August 02, 2004: Message edited by: Jack Kay ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by smyle khanna:
Hi..

I want 2 know y do v use finally block in exception handling when the statements after the try catch block can b executed without a finally block n irrespective of a exception.
thanks
Smyle


Let's try to translate this into English...
y == why
v == you ???
b == be
n == ???

and then there's the little area of punctuation which comes in really handy when writing in other languages from Latin where it was considered to be more or less optional and often not used to control the flow of the sentence leading to phrases that are difficult if not impossible for the reader to properly comprehend without rereading multiple times.


42
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
You can get a good explanation here.


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do v use finally block in exception handling?