Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
Im learning about exceptions and having doubt in that.
Im clear in try and catch but dont know what is the use of finally in it...also cant get the difference between catch and finally,guide with small example
Posted

The catch clause simply allows you to handle exceptions that your code may throw. The code inside the catch braces is only executed in case an exception is thrown.
The code inside the finally braces is always executed which allows you to clean up resources in order to e.g. avoid memory leaks.
C#
try
{
  //code that may throw exceptions
}
catch (Exception ex)
{
  //Exception handler
}
finally
{
  //Code that needs to be executed to clean up the prior code (e.g. to avoid memory leaks and to free resources)
}
 
Share this answer
 
Comments
Mohamed Mitwalli 19-Jun-12 1:49am    
5+
Sergey Alexandrovich Kryukov 19-Jun-12 2:00am    
It's good enough, my 5, but one note would be useful: "Code that needs to be executed to clean up the prior code" is a very typical use (it was useful to give such explanation), but not exactly in 100% cases.
--SA
As soon as you don't know what "finally" does, you cannot maintain that you are clear about try-catch. The finally block is placed at the end of try-catch-finally or try-finally construct and is executed in all possible cases: if exception was thrown in a try block or not, if it was caught or not, if catch blocks exist or not. As simple as that.

The small example you wanted can be found here:
http://msdn.microsoft.com/en-us/library/dszsf989%28v=vs.100%29.aspx[^].

It's wonderful that you did not find it by yourself; it would save not only my or other expert time, but even your time. Certain things are much easier to find in documentation compared to asking questions.

Besides, I would advise you to pay attention for correctness of your questions: the question of the form "what is the difference between {0} and {1}?" is almost never correct; and it is not correct in your case. If you still did not get it, please try to tell us: what is the difference between apple and Apple?

Please also take a look at my past answer: what is the difference between the class and encapsulation in programming[^].

And please see this discussion on asking good questions:
How to ask a good question?[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 19-Jun-12 1:46am    
Good explanation + suggestion SA, +5!
Sergey Alexandrovich Kryukov 19-Jun-12 1:57am    
Thank you very much, Prasad.
Sorry that I had to give you a low vote, but I pointed out essential inaccuracy. Besides, it's better not to follow incorrect question in the answer and try to tell "what the difference". There is no "difference" in things which similarity is questionable.
--SA
Prasad_Kulkarni 19-Jun-12 2:05am    
Yep Sure
Mohamed Mitwalli 19-Jun-12 1:49am    
5+
Sergey Alexandrovich Kryukov 19-Jun-12 2:05am    
Thank you, Mohamed.
--SA
Hi,

Please follow the link to learn more about try-catch and finally:

http://msdn.microsoft.com/en-us/library/s7fekhdy%28v=vs.71%29[^]

All the best..
--AK
 
Share this answer
 
Comments
Mohamed Mitwalli 19-Jun-12 1:52am    
5+
[no name] 19-Jun-12 1:59am    
thanks mohamed.. :)
 
Share this answer
 
Comments
Mohamed Mitwalli 19-Jun-12 1:50am    
5+
Sergey Alexandrovich Kryukov 19-Jun-12 2:00am    
Good references, my 5.
--SA
Finally is executed no matter what.
So, if your try block was successful it will execute, if your try block fails, it will then execute the catch block, and then the finally block.

The big difference is that try...catch will swallow the exception, hiding the fact that an error occurred.

try..finally will run your cleanup code and then the exception will keep going, to be handled by something that knows what to do with it.

Ref.:try {…} finally {…}; try {…} catch{}[^]
-You can get more detailed description also, have a look.
 
Share this answer
 
Comments
Mohamed Mitwalli 19-Jun-12 1:49am    
5+
Sergey Alexandrovich Kryukov 19-Jun-12 1:53am    
About try-catch -- not correct at all.

In most cases (except the top of the stack if each thread and the main UI event handling loop), it is used to process exception and throw another one, usually of the more semantic character. For this purpose, the new exception is usually thrown, but there is also a re-throw statement, written as throw without parameters. It is usually used when some side effect of the condition which caused exception (not exception itself) should be compensated, but the information on the exception still should be considered somewhere up the stack, for example, displayed. This is more rare case.

As to the "swallowing" the exceptions, in 99% of beginner's code this is just the result of misunderstanding of how exceptions work. "Swallowing" exception is rarely used in correct code, mostly as a work-around of some "bad" code which throws unexpected or unreasonable exceptions on some non-exceptional conditions.

Generally, the best practice of using exceptions is: throw them and almost never catch. It should be done only on top of stack or main UI loop. Most exceptions caught by inexperienced developers should not be caught. This is the whole idea about exception: throw and forget.

After some thinking, I decided to vote 3, sorry. The link is also pretty bad. You should not trust other answers only because they are on CodeProject or StackOverflow and answer only if you are know the matter very well and sure about it.

--SA
Prasad_Kulkarni 19-Jun-12 2:02am    
I wish next time you will not think while voting :D jokes apart;

Thanks for spending time to give such a nice suggestion and explanation too for me. I really won't mind for voting.

I will apply surely on your suggestions now onwards.
have look this very good Article[^]
 
Share this answer
 
Hi friend,

Plese check the folloeing links...



For TRY:http://www.dotnetperls.com/try[^]


For Finally : http://www.dotnetperls.com/finally[^]
 
Share this answer
 
Whenever There is Exception in Code within try block control will be directed to catch block to handle the exception . However finally block will be executed always. Generally Resource clean up code should be written in finally block, so that whether Exception arises or not Resource need to be free.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900