Response.Redirect in a Try-Catch

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I've noticed that sometimes putting a Response.Redirect in a Try-Catch will itself throw an exception due to a threading violation. However, in some cases it works, in others it doesn't, and I can't pre-determine which is which.

Does anyone know how to ensure that the Response.Redirect doesn't cause threading problems?

Thanks,
Tim
 
Response.Redirect(False)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Tim said:
Hello,
I've noticed that sometimes putting a Response.Redirect in a Try-Catch
will itself throw an exception due to a threading violation. However, in
some cases it works, in others it doesn't, and I can't pre-determine which
is which.
 
This is because it throws an exception (this is by design). This is
actually how it manages to stop the current thread - by throwing a special
kind of exception, which the runtime knows is meant to stop the current
thread of execution.

What this means, is that you can't have it in a try catch block.

Tim said:
Hello,
I've noticed that sometimes putting a Response.Redirect in a Try-Catch
will itself throw an exception due to a threading violation. However, in
some cases it works, in others it doesn't, and I can't pre-determine which
is which.
 
What this means, is that you can't have it in a try catch block.

Sure you can:

try
{
Response.Redirect(someUrl);
}
catch{}

This is not as elegant as the parameter method of repressing the error, as
this will cause the exception-handling in .Net to process the error, and
affect performance, but it will certainly work to repress the error.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Thanks... I looked at the second overload method for Response.Redirect
why would you use false instead of true (you want the execution to stop in order to prevent an error?)
would you just code it like so:

try {
//do stuff...
} catch {
Response.Redirect('myPage.aspx', true);
}

Thanks
 
Sorry about leaving the URL parameter out of my example! You set it to
false, because if you set it to true it will cause an exception. I realize
the documentation is a bit ambiguous regarding this, but that's what works.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top