RSS

Response.Redirect Try catch block , ThreadAbortException


What is with the combination of Response.redirect and Try catch block?



well, most of you who have tried out the combination must have been stuck with a weird error of "Thread was being aborted" and must have wondered what has a thread got to do with Response.redirect?



Well, to answer your question here goes:-

A thread is executing your application in terms of ASP.NET worker process , when you call Response.Redirect(URL);

In your code then to redirect to the new URL specified by you ASP.NET framework must be told to stop the current execution of the page and to transfer the execution to the URL page specified in the method call.

This is done in a 2 way step :-

1) Response.End() is called internally by Response.Redirect to stop the current execution and the ThreadAbortException is thrown.

2) .NET framework will call catch “ThreadAbortException” and stop current execution and start executing the new page.

Now during the Step #1 Response.End will throw out a ThreadAbortException” to let .NET framework know that the current execution needs to be stopped and the execution of new should begin.

Asp.net framework catches the Redirect method's exception, aborts the thread and use a new thread for execution of to be redirected page.



Solution :- The way to get over this problem is to specify Response.Redirect(URL,false) , this will tell .NET framework not to stop the execution of the current thread and hence the error will be resolved.

ThreadAbortException” is a special kind of an exception even if you catch it in a catch block even then it will be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before killing the thread.
  1. Wenancelvekat

    September 30, 2008 at 5:45 AM

    Hello
    Nice site!

    Bye

  1. Wenancelvekat

    September 30, 2008 at 5:45 AM

    Hello
    Nice site!

    Bye

  1. Dipen Bhadra

    October 28, 2008 at 4:42 AM

    good answer, Post more comments...

  1. Shaunak Pandit

    February 21, 2009 at 1:31 AM

    Thanks Dipen... but comments on what?

  1. Quang Nguyen

    April 23, 2009 at 2:31 AM

    Thanks a million, you've helped me a lot.

  1. Quang Nguyen

    April 23, 2009 at 2:31 AM

    Thanks a million, you've helped me a lot.

  1. Alisson Santana

    May 5, 2009 at 8:57 AM

    Hi, Pandit.
    That page that we left "executing" ('cause we explicit told to asp .net to keep executing it), will still be consuming resources on server or it will be unloaded in a undetermined time?
    Do you know something about these workflow ?

  1. Alisson Santana

    May 5, 2009 at 8:57 AM

    Hi, Pandit.
    That page that we left "executing" ('cause we explicit told to asp .net to keep executing it), will still be consuming resources on server or it will be unloaded in a undetermined time?
    Do you know something about these workflow ?

  1. laulsescout

    August 13, 2009 at 9:24 PM

    This look interesting,so far.
    If it's not just all bots here, let me know. I'm looking to network
    Oh, and yes I'm a real person LOL.

    See ya,

  1. laulsescout

    August 13, 2009 at 9:24 PM

    This look interesting,so far.
    If it's not just all bots here, let me know. I'm looking to network
    Oh, and yes I'm a real person LOL.

    See ya,

  1. Shaunak Pandit

    August 16, 2009 at 10:59 AM

    well these are live people blogging not bots :) thoh yeah been out of touch of the blog for a while hope to get back up and rolling...

  1. Shaunak Pandit

    August 16, 2009 at 10:59 AM

    well these are live people blogging not bots :) thoh yeah been out of touch of the blog for a while hope to get back up and rolling...

  1. Shaunak Pandit

    August 16, 2009 at 11:06 AM

    Alisson unfortunately havent looked into it in that detail. but my guess is if we keep it executing it should be consuming resources

  1. Shaunak Pandit

    August 16, 2009 at 11:06 AM

    Alisson unfortunately havent looked into it in that detail. but my guess is if we keep it executing it should be consuming resources

  1. Kamlesh

    August 21, 2009 at 10:49 AM

    thanks your soln works.....helped me a lot.......

  1. Exception Handling .NET Try Catch Finally Block working « In the Line of Fire….Shaunak Pandit

    August 25, 2009 at 12:49 PM

    [...] a Appointment programm…Jenkins on Sending a Appointment programm…Shaunak Pandit on Response.Redirect Try catch bl…Kamlesh on Response.Redirect Try catch bl…Shaunak Pandit on Response.Redirect Try catch [...]

  1. Exception Handling .NET Try Catch Finally Block working « In the Line of Fire….Shaunak Pandit

    August 25, 2009 at 12:49 PM

    [...] a Appointment programm…Jenkins on Sending a Appointment programm…Shaunak Pandit on Response.Redirect Try catch bl…Kamlesh on Response.Redirect Try catch bl…Shaunak Pandit on Response.Redirect Try catch [...]

  1. Prashant Deshmane

    March 10, 2011 at 1:46 PM

    Nice explaination.

    No one on the net has explained so much in detail

  1. Prashant Deshmane

    March 10, 2011 at 1:46 PM

    Nice explaination.

    No one on the net has explained so much in detail

  1. Barbeddiviaro

    September 22, 2011 at 8:44 AM

    Man .. Excellent .. Amazing .. I'll bookmark your blog and take the feeds additionallyI'm satisfied to find so many useful information here within the put up, we need develop extra strategies on this regard, thank you for sharing. . . . . .

  1. Barbeddiviaro

    September 22, 2011 at 8:44 AM

    Man .. Excellent .. Amazing .. I'll bookmark your blog and take the feeds additionallyI'm satisfied to find so many useful information here within the put up, we need develop extra strategies on this regard, thank you for sharing. . . . . .

Post a Comment

Copyright © Shounak S. Pandit. Powered by Blogger.

Ads