While doing some work today I started to run into a ThreadAbortException error. While debugging it spit back this error:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
I was really confused because everything seemed to be working just fine, then I found this forum thread. Turns out there are several causes for this error, but mine was caused by doing a Response.Redirect(); from within a Try-Catch statement. Apparently ASP .NET does not like that.
Workaround
In a try-catch statement, use a boolean variable to mark weather or not to Redirect (or some other variable to accomplish your logic) and preform the Response.Redirect(); outside of your Try Catch. On a side note, I’ve successfully been able to Redirect from inside the Catch {} aspect, just not the try {}.
Resources
Here is the link to the forums that I found my answer:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=268544&SiteID=1
Hey Justin,
Saved me getting into a vicious circle. I have finished working on my website and it is getting ready to go to production and guess what I discover this issue. My problem is exactly as yours redirecting from inside a try block. Thanks a ton.
Regards
LikeLike
Thanx it helped me too
LikeLike
thank u u helped me also
LikeLike
thanks By Justin Carmony . good nice post.
LikeLike
Thank you. I was scratching my head at this error!
LikeLike