Response.Redirect in a Try-Catch

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
 
K

Kevin Spencer

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.
 
M

Marina

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.
 
K

Kevin Spencer

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.
 
G

Guest

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
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top