Will code in a Finally statement fire if I return a value in a Try block?

JamesEggers picture JamesEggers · Dec 5, 2008 · Viewed 78.2k times · Source

I'm reviewing some code for a friend and say that he was using a return statement inside of a try-finally block. Does the code in the Finally section still fire even though the rest of the try block doesn't?

Example:

public bool someMethod()
{
  try
  {
    return true;
    throw new Exception("test"); // doesn't seem to get executed
  }
  finally
  {
    //code in question
  }
}

Answer

Andrew Rollings picture Andrew Rollings · Dec 5, 2008

Simple answer: Yes.