Sunday, September 19, 2004

Redirecting in the Catch Block

One of my good friend told me that you cannot Redirect in catch without doing something special. Try writing some code that throws an exception and than in the catch block redirect it to some new page. Something like this:

try {
// write some code that creates an exception
}

catch(Exception ex)
{
// Redirect to other page
Response.Redirect("NewPage.aspx");
}

If you try to do this it will throw System.ThreadingException. In order to do this successfully you need to redirect like this:

Response.Redirect("NewPage.aspx",false);

Here false means that the old page thread will not be discarded and will be removed later on by the GC. Another approach can be using an instance of System.Threading and stopping the thread before the Redirection.


No comments: