I am using try catch and finally blocks in my project. my doubt here is... in why we needs to use finally block, actually if not use finally block then also the codes will execute after catch block. so we can do codes(to do resource free) after catch block. this will execute even if exception has occured. So is there any advantage if we use finally block?
-
possible duplicate of [Why use finally in C#?](http://stackoverflow.com/questions/547791/why-use-finally-in-c) – John Saunders Jul 28 '11 at 04:49
7 Answers
According to the answer of this previous SO Post:
The code inside a finally block will get executed regardless of whether or not there is an exception. This comes in very handy when it comes to certain housekeeping functions you need to always run like closing connections.
I imagine you are talking about this:
try {
// do something that might throw exception
}
catch (Exception) {
// take care of exception
}
finally {
// what is the point of this?
}
If you handle all possible exceptions in the catch
block, the only reason why you might want a finally
block is to intercept return
statements from within the try
block.
Thanks to @dthorpe: finally
will also intercept any exception/return statement from within the catch
block.

- 5,765
- 5
- 27
- 36
-
3That finally will also execute if an exception is thrown from the catch block. – dthorpe Jul 01 '11 at 06:39
Code will execute in a catch block only if an exception is thrown.
Code will execute in a finally block if an exception is thrown, and when no exception is thrown. Since you can rely on the finally block to always execute regardless of whether an exception was thrown or not, finally blocks are where you should clean up any resources you allocated, particularly non-managed resources such as file or OS handles.
You mention relying on execution of code after a catch block to release resources. This is not a safe assumption, since a) a properly constrained catch block does not catch all exceptions, but just the exceptions that the code needs to deal with and b) the catch block itself can throw or rethrow an exception. The code following the catch block might not be executed.
If you are in the habit of writing unqualified catch blocks that capture all exceptions and kill them, you're abusing exceptions.

- 35,318
- 5
- 75
- 119
-
+1 for explaining about the abuse and that catch/finally are two fundamentally different things. Boy I wish more languages would disallow catch and finally in the same statement... – Jeroen Wiert Pluimers Jul 05 '11 at 16:02
You should use the finally block to de-allocate unmanaged resources in the event of an exception. You could also handle GC or Dispose any disposable type constructed in the "try"

- 10,290
- 3
- 42
- 73
Finally
Block gets executed even exception
occurs or not. So you could close the Database Connection
or other Dispose
activity in finally
block.

- 6,537
- 2
- 25
- 38
Imagine this use:
String doSomething()
{
SqlConnection conn;
try {
conn = new SqlConnection();
// SQL stuff
return result;
}
catch(Exception ex) { AddToLog(ex); }
finally {
if (conn != null)
conn.Dispose();
}
return null;
}
No matter what happens in the SQL part, you will always dispose correctly.

- 4,724
- 7
- 29
- 49
Well it actually depends on how you are using it, if you have only empty catch blocks then it won't make any difference if you have finally block or not. Its very elegant in cases where you are handling somethings in the catch block and also desire that some code needs to executed post the catch also.
However, try/catch/finally is for exceptional circumstances and needs to be planned for the worst to make your program more robust. if one of the catch handlers rethrew an exception, the code following the catch block (not in a finally block) would never be executed.
Since there are high chances for code in some specific scenarios to not be executed, a finally block is your insurance for executing those critical actions you need. You will need to identify such cases and there is no such rule that every try catch block must have a finally.

- 37,194
- 9
- 78
- 82