tags:

views:

208

answers:

10

Take for example the following code:

   try
   {
      Response.Redirect(someurl);
    }
    finally
    {
       // Will this code run?
    }

Will the code in the finally block run?

+9  A: 

Yes.

Try it and see!

Neil Moss
Haha, teach a man to fish... ;)
Dan Tao
.....and you'll soon find you should have taught him how to swim first :)
Paul Hadfield
+4  A: 

Simple enough to test:

try
{
  Response.Redirect(someurl);
}
finally
{
   File.WriteAllText("C:\\Temp\\test.txt", "The finally block ran.");
}
ChaosPandion
A: 

Why do you not just try it?

finally always runs, except in these extreme scenarios:

  • Total application crash, or application termination (e.g. FailFast())
  • A limited number of serious exceptions
  • Threads getting terminated (eg. Thread.Abort())
  • Hardware failure (e.g. machine losing power)
  • Infinite loop inside the try-block (which ultimately results in application termination)
abelenky
Oh no, not this argument again :) There are several instances in which a "finally" block would not run. It's been discussed before: http://stackoverflow.com/questions/3216046/does-the-c-finally-block-always-execute
David
Edited answer to be a bit more precise. Still, except in EXTREME cases, finally *always* runs.
abelenky
A: 

Yes. Code in the finally is guaranteed to run, unless something catastrophic happens.

Brian Genisio
A: 

Yes. Here is how you can check if I am right or not. Simply place a message box or write something to the console from finally and you will have your answer.

Adkins
+2  A: 

It will indeed. See this MSDN article: Finally always executes

Dave McClelland
"Finally always executes." Well, not _always_. http://stackoverflow.com/questions/3216046/does-the-c-finally-block-always-execute
David
@David Touche. Finally always executes, barring a catastrophe beyond the CLR's control. Better? :)
Dave McClelland
@Dave: It's just a fun subject to contemplate, isn't it? I may use it as an interview question the next time I find myself doing a technical pre-screen on a candidate: "Does a 'finally' block always guarantee execution, and if not then in what cases wouldn't it?" So far I can think of 7 distinct (though some of which are closely related) scenarios that would prevent it.
David
@David I posted the scenario where try and finally have differing return values on the whiteboard in my office. It ended up nerd-sniping more coworkers than I was expecting and generated a lot of interesting conversation. What are your 7 scenarios? Everything I can think of involves either forcefully killing a running process or removing the machine's power source. I'm very curious to hear some more, though.
Dave McClelland
@Dave: StackOverFlowException, OutOfMemoryException, ExecutingEngineException, Environment.FailFast(), Process/Thread is killed by an external process/thread (not thread abort, which will attempt to execute the finally block, but a low-level termination of the thread with extreme prejudice), Infinite loop within the try block (most likely a precursor to the previous one), and power failure.
David
@David Very interesting - I'll keep this thread in mind the next time my coworkers get into a discussion on ways to royally screw up our code (in theory, of course :))
Dave McClelland
+4  A: 

It will run. Response.Redirect actually throws a ThreadAbortException, so that's why code after that will not run (except anything in a finally block of course).

Philippe Leybaert
+1  A: 

The code in the finally will run, but it will run before the redirect, since the redirect won't be sent to the browser until the method returns, and the finally code will execute before the method returns.

Ned Batchelder
+1  A: 

Try this:

try
{
  Response.Redirect("http://www.google.com");
}
finally
{
   // Will this code run?
  // yes :)
  Response.Redirect("http://stackoverflow.com/questions/3668422/will-code-in-finally-run-after-a-redirect");

}
Zafer
A: 

The general rule is that the code in finally will be applied in all cases (try/catch)

abdelrahman ELGAMAL