• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do v use finally block in exception handling?

 
smyle khanna
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get a good explanation here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic