9

Take for example the following code:

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

Will the code in the finally block run?

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252

10 Answers10

13

Yes.

Try it and see!

Neil Moss
  • 6,598
  • 2
  • 26
  • 42
7

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
  • 168,566
  • 31
  • 210
  • 223
6

Simple enough to test:

try
{
  Response.Redirect(someurl);
}
finally
{
   File.WriteAllText("C:\\Temp\\test.txt", "The finally block ran.");
}
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
5

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

Dave McClelland
  • 3,385
  • 1
  • 29
  • 44
  • 1
    "Finally always executes." Well, not _always_. http://stackoverflow.com/questions/3216046/does-the-c-finally-block-always-execute – David Sep 08 '10 at 14:11
  • @David Touche. Finally always executes, barring a catastrophe beyond the CLR's control. Better? :) – Dave McClelland Sep 08 '10 at 18:41
  • @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 Sep 08 '10 at 18:50
  • @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 Sep 08 '10 at 19:27
  • 1
    @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 Sep 08 '10 at 19:39
  • @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 Sep 08 '10 at 19:47
3

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
  • 63,815
  • 23
  • 109
  • 159
  • 2
    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 Sep 08 '10 at 14:03
  • Edited answer to be a bit more precise. Still, except in EXTREME cases, finally *always* runs. – abelenky Sep 08 '10 at 17:26
3

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
  • 364,293
  • 75
  • 561
  • 662
3

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
  • 2,180
  • 16
  • 28
2

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

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
2

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.

Christopher B. Adkins
  • 3,499
  • 2
  • 26
  • 29
2

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

Abdelrahman ELGAMAL
  • 414
  • 2
  • 7
  • 15