views:

169

answers:

7

Possible Duplicate:
Why use finally in C#?

In C#, what is the point of having a finally clause?

eg.

try {
        // do something
    }
catch (Exception exc)
    {
        // do something
    }
// do something

Won't the code at the end execute anyway? What is the point of a finally block?

A: 

There are pieces of your code that you want to execute regardless of the success of previous code. By using Try/Catch/Finally you are able to benefit from the error handling.

websch01ar
+2  A: 

Finally is for the event that even the catch throws an exception, plus it allows you to exewcute code on success and failure, the finally will ALWAYS be executed. ALWAYS.

Ok, except when the application is killed at the system level or the computer explodes.

Jimmy Hoffa
-1: ALWAYS is a strong word. StackOverflowException?
Moron
@Moron: true, fixed.
Jimmy Hoffa
@Moron: Always may, indeed, be a strong word, but it's the only one that wouldn't have required an additional paragraph of explanation, and it gets the point across.
AllenG
@AllenG: Disagree. I would prefer more explanation than having something which is incorrect!
Moron
-1StackOverflowExceptionExecutingEngineException (on a FailFast()).Both do not execute the finally block.This is a site for answers to questions, not for misleading information that may eventually hurt the user.
DarkBobG
I don't know...if the process gets killed or the computer explodes, I still have faith that the finally block will execute somewhere...somehow...in the mysterious realm of the software afterlife.
Dr. Wily's Apprentice
A: 

Because there are certain objects that you should perform cleanup on, otherwise it will cause issues for your application.

The constantly used example is using a SqlConnection:

SqlConnection conn = new SqlConnection(connString);

try
{        
    conn.Open();

    throw new ArgumentException();
}
catch(SqlException ex)
{
}

In this case, the SqlConnection is left open with no way of closing it because you handle a SqlException, but ArgumentException is thrown. If you used a finally block, this wouldn't happen because the finally block code would execute:

try
{        
    conn.Open();

    throw new ArgumentException();
}
catch(SqlException ex)
{
}
finally
{
    conn.Dispose();
}
Justin Niessner
A: 

The finally block is garaunteed to run when an exception is caught, even if the exception handling block throws more exceptions. Often used for cleaning up certain kinds of resources such as open file handles, network connections, etc...

Good examples here:

http://dotnetperls.com/finally

http://www.csharp-station.com/Tutorials/lesson15.aspx

FrustratedWithFormsDesigner
+1  A: 

The finally ensures that anything in the block will be executed, regardless if the previous two statements are activated.

A good example would be to release database resources.

Try-Catch-Finally

Example:

try
{
   //Open a database connection
}
catch
{
   //Catch exception, database connection failed
}
finally
{
   //Release the opened database connection resources
}
Jamie Keeling
A: 

The Finally ensures that whatever is in it will fire if the try works or fails. In a scenario where a catch occurs you are not guaranteed that everything at the last //do something will occur.

klabranche
A: 

The code in the Finally block always executes, regardless of if an exception was thrown or not. The code following the exception block may or may not execute if the path returns in the catch.

Jon