6

I've try catch finally block and if some exception occurs I'll return from the catch block, so finally block is still executed, if so, when? Before return or after return?

Is this the right practice?

try
{
// do something
}

catch (Exception)
{    
  return false;
}
finally
{
  if (connection.State == ConnectionState.Open) connection.Close();
}
JPReddy
  • 63,233
  • 16
  • 64
  • 93

4 Answers4

6

It will execute "finally" block after return. "Finally" is used for some practice such as close database connection (always need to be done)

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
  • One would think that finally is not needed if you use `using`. – Dialecticus Dec 08 '10 at 10:32
  • using can only be applied to instances that implement the IDisposeable interface and the compiler will transform the using block to an adequate try...finally block. Edit: Replaced ICloneable with IDisposable – Dennis Dec 08 '10 at 10:35
  • @Dialectus: that is a reasonable assumption, given that using is syntactic sugar that makes the compiler issue a call to `Dispose` with a `finally` block. – Fredrik Mörk Dec 08 '10 at 10:36
  • @Dennis: Isn't that IDisposable? – Dialecticus Dec 08 '10 at 10:36
  • @Dialecticus: Yupp, Sorry. Ten minutes ago I implemented the ICloneable interface for one of my classes. Looks like it wouldn't get out of my mind – Dennis Dec 08 '10 at 10:39
  • 2
    @Dennis: you are aware that it is not recommended to use ICloneable? See here: http://stackoverflow.com/questions/699210/why-should-i-implement-icloneable-in-c – Fredrik Mörk Dec 08 '10 at 11:01
2

finally block is always executed. In your case it is executed before your return statement.

Nicolas
  • 6,289
  • 4
  • 36
  • 51
  • I think finally is executed after return statement, but before the execution goes back to calling function. return -> finally -> go back. – Dialecticus Dec 08 '10 at 10:35
  • Debbuger goes to return statement first and next to the finally block. But the program goes to the finally block first and after it leaves the method. So I can reasonably say that the finally block is executed before the return (leave the method) statement. – Nicolas Dec 08 '10 at 10:50
  • 5
    The C# Language Specification is quite clear on the order in which things happens: 1) the expression in the `return` statment is evaluated, 2) the `finally` block is executed (this is repeated for any enclosing `try-finally` blocks), 3) control is returned to the caller. So, `finally` is executed *after* the `return` statement is evaluated, but *before* control is returned to the caller. – Fredrik Mörk Dec 08 '10 at 11:29
  • @Fredrik: That would have been answer instead of comment. – JPReddy Dec 09 '10 at 09:27
1

You can try with your self

private bool test()
    {
        try
        {
            int i = 0;
           int u = 10 / i;
        }

        catch (Exception)
        {
            return false;
        }
        finally
        {

        }
        return true;
    }

so it is a divideby zero exception. When you execute this code , finally will execute and after return will execute.

it is something like Runtime the returned result in case of finally block!

TalentTuner
  • 17,262
  • 5
  • 38
  • 63
0

A finally block will always execute before the code exits a try-catch-finally block (any condition like a ThreadAbortException which prevents the finally block from executing will prevent code from exiting the try-catch-finally block).

supercat
  • 77,689
  • 9
  • 166
  • 211