+6  A: 

A finally block will always be executed and this will happen before returning from the method, so you can safely write code like this:

try {
    return "foo";
} finally {
    // This will always be invoked
}

or if you are working with disposable resources:

using (var foo = GetFoo())
{
    // foo is guaranteed to be disposed even if an exception is thrown
    return foo.Bar();
}
Darin Dimitrov
Thank you for your answer!
Liu
+13  A: 

Yes, the finally block is executed however the flow leaves the try block - whether by reaching the end, returning, or throwing an exception.

From the C# 4 spec, section 8.10:

The statements of a finally block are always executed when control leaves a try statement. This is true whether the control transfer occurs as a result of normal execution, as a result of executing a break, continue, goto, or return statement, or as a result of propagating an exception out of the try statement.

(Section 8.10 has a lot more detail on this, of course.)

Note that the return value is determined before the finally block is executed though, so if you did this:

int Test()
{
    int result = 4;
    try
    {
        return result;
    }
    finally
    {
        // Attempt to subvert the result
        result = 1;
    }
}

... the value 4 will still be returned, not 1 - the assignment in the finally block will have no effect.

Jon Skeet
Thanks for your answer!
Liu
not that it is really of much practical significance but the finally block is not executed if a StackOverflowException occurs or if you call System.Environment.FailFast(...)
Patrick Klug
Of course, control doesn't return to the caller at all in those cases. You could say that flow *ends* rather than leaving the block, so that Skeet's statement still holds.
Ben Voigt
+1  A: 

Will finally blocks be executed if returning from try or catch blocks in C-Sharp?

YES

If it will,Before returning or after?

BEFORE

priyanka.sarkar_2
+3  A: 

With two-pass exception handling, which .NET inherits from windows, you can't precisely say that the finally block executes before control passes back to the caller.

The finally block will execute after finally blocks in more nested call frames, and before finally blocks and the catch block in less nested call frames, which is consistent with the finally block running before returning. But all exception filters between the throw point and the catch point will run before any finally blocks, which means that in the presence of an exception some caller code can run before the finally block.

When control leaves the block normally (no exception thrown), then the finally runs before control returns to the caller.

Ben Voigt
@Ben: +1 because exception filters are worth mentioning... but even with exception filters, surely the finally block is running before control passes back to the caller. It's just that exception filters can run even before that.
Jon Skeet
Well, control passes to the caller's exception filter, then to the finally block, then to the caller's exception handler. So control passes to the caller both before and after the finally block.
Ben Voigt