views:

563

answers:

5

I got some weird error with response.redirect and the project wasn't building at all.. when I removed the try-catch block that was surrounding the block of code where Response.Redirect was in it worked normally..

Just want to know if this is a known issue or something...

+11  A: 

If I remember correctly, Response.Redirect() throws an exception to abort the current request (ThreadAbortedException or something like that). So you might be catching that exception.

Edit:

This KB article describes this behavior (also for the Request.End() and Server.Transfer() methods).

For Response.Redirect() there exists an overload:

Response.Redirect(String url, bool endResponse)

If you pass endResponse=false, then the exception is not thrown (but the runtime will continue processing the current request).

If endResponse=true (or if the other overload is used), the exception is thrown and the current request will immediately be terminated.

M4N
+3  A: 

As Martin points out, Response.Redirect throws a ThreadAbortException. The solution is to re-throw the exception:

try  
{
   Response.Redirect(...);
}
catch(ThreadAbortException)
{
   throw; // EDIT: apparently this is not required :-)
}
catch(Exception e)
{
  // Catch other exceptions
}
Philippe Leybaert
You don't actually need to re-throw the ThreadAbortException. The runtime will automatically resurrect it anyway at the end of the catch block. See the remarks section of http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx
LukeH
There is no need to rethrow this exception, after each try catch block has handled this exception, the exception is rethrown by the CLR anyway. You can only stop this by calling Thread.ResetAbort() but I wouldn't recommend it.
AnthonyWJones
Ok :-) But you still need to catch the ThreadAbortException separately (or check the type of the exception manually)
Philippe Leybaert
The KB article that Nathan Koop links has the recommended way to handle this, by calling a different overload of the Response.Redirect() method.
Greg D
+3  A: 

Martin is correct, a ThreadAbortException gets thrown when you use a Response.Redirect, see the kb article here

Nathan Koop
A: 

I don't think there is any known issue here.

You simply can't do a Redirect() inside a try/catch block because Redirect leaves the current control to another .aspx (for instance), which leaves the catch in the air (can't come back to it).

EDIT: On the other hand, I might have had all of this figured backwards. Sorry.

tzup
That's not correct. When calling Response.Redirect(), a ThreadAbortException is thrown, which can be caught.
Philippe Leybaert
Yeah, forgot to think while I was typing :), my apologies.
tzup
A: 

You may have referenced a variable that is declared inside the try block.

For example, the below code is invalid:

try
{
  var b = bool.Parse("Yeah!");
}
catch (Exception ex)
{
  if (b)
  {
    Response.Redirect("somewhere else");
  }
}

You should move out the b declaration to outside the try-catch block.

var b = false;
try
{
  b = bool.Parse("Yeah!");
}
catch (Exception ex)
{
  if (b)
  {
    Response.Redirect("somewhere else");
  }
}
Adrian Godong