Ex = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

asp.net

I understand that if you put a Response.Redirect inside a try-catch that you're going to get this error unless you specify the 2nd param of the redirect as false.

But even looking at this article (PRB: ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer) I still don't understand why I have to set this to false for this particular line of code…we've always had true for that param until I wrapped that in a try-catch:

Response.Redirect(SecureUrl("Confirmation", SessionID), true);

We want to close it because it's the end of the line..the confirmation page. But when this is wrapped in the try-catch I get that error. I just want to understand better why false. I read the article and it doesn't jump out at me.

Best Answer

If you pass true as the second parameter, it will throw a ThreadAbortException to stop processing the request.

Code inside of ASP.Net will catch the ThreadAbortException, call Thread.ResetAbort, and send the (HTTP 301) response.

If you have a catch block, you will also see the ThreadAbortException, just as you'd see any other exception.

The best thing for you to do is to add an empty catch block for ThreadAbortException before your catch block, like this:

} catch(ThreadAbortException) { throw; }