Wednesday, April 18, 2007 3:24 PM
Geoff
Response.Redirect and ThreadAbortException
I have just spent two days chasing down a production bug that didn't occur in the development environment. When I had given up and decided to pepper the source with logging and put in a few judicious empty Try/Catch blocks I finally stumbled across the error. AND IT WASN'T ME!!! Sort of.
The short of it all is, Response.Redirect internally calls Response.End. Response.End ends the response thread by aborting it. Thread.Abort aborts a thread by throwing a ThreadAbortException.
Somehow, in this particular instance, the ThreadAbortException was bubbling to the calling function and causing it to fail in production. But not in development. And not anywhere else. See, Response.Redirect had been called a least a half-dozen times in the code before the problem one was called, and without any exceptions being thrown. Go figure.
The fix is to either wrap the Response.Redirect call in an empty Try/Catch that's aimed specifically at the ThreadAbortException, or call the second overload of Response.Redirect that allows you to specify whether Response.End should be called or not.