“Thread was being aborted” error when using Response.Redirect
This one caused me a lot of grief over the past week or two. A page in my latest ASP.NET project had begun to intermittently throw the following error: “Thread was being aborted”. I tracked the error down to a part of the code where I was doing a Response.Redirect(), which looked something like this:
try
{
// do some stuff
...
Response.Redirect(url);
}
catch()
{
// handle exceptions
...
}
Well, it turns out that the Response.Redirect call within the try .. catch block will throw the “Thread not aborted exception”, and according to Microsoft this behaviour is by design - see http://support.microsoft.com/default.aspx?scid=kb;en-us;312629.
I must admit, their explanation makes no sense to me. Why ending a request in the middle of the try.. catch block should be a problem is not clear - I can certainly see that it is inelegant, but I am not sure where the exception is coming from. Also, the weird thing is that in my case the exception was only thrown intermittently, about 10% of the time that the code ran. Surely, if it was a violation of the ASP.NET request/response cycle in some way, it should happen consistently?
Anyway, when developing code to timelines and budgets, you sometimes have to accept that you won’t have time to 100% understand the fix that you have found, even though you need to use it. In this case, there are two things I needed to do to fix this. Firstly, I moved the Response.Redirect out of the try .. catch block. Secondly, just to make sure, I used the Response.Redirect(url, false) overload, which does not abort the running thread when it launches the new one.

It does through exception even if you have the Response.Redirect(url); in try-catch block. Its just that you are catching the exception in your catch block and doing nothing about it(you are eating it up!!!)
i.e., it always throws the exception because it is aborting the request. You could do Response.Redirect(redirectUrl, false) not to throw exception but it splits out the html behind the scenes…
check this entry…
http://www.c6software.com/articles/ThreadAbortException.aspx
Comment by budigelli — November 25, 2008 @ 10:50 pm
RESPONSE.REDIRECT(URL,FALSE) havent solved my problem of getting thread is being aborted exception.
Any other advice in order to get out off this problem.
Comment by vaishali — April 11, 2009 @ 6:42 am
Its simple, pls try this. it will work, catch specific thread about exception and leave the catch block empty. it would not bumborad ur eventviewer
Comment by Saravanan Sundaresan — April 24, 2009 @ 1:14 pm