Response.Redirect gens ThreadAbortException that's either not being caught or thrown a second time.

E

Eric

I have the following situation: I was getting intermittent errors using
Reponse.Redirct("url", true) and was trying to catch the
ThreadAbortException, but it was not staying caught and was showing up again
where my code wasnt expecting it. I'm not sure what was causing it. The
catch block in RedirectPage is supposed to quash the ThreadAbortException
but it not only shows up there, but also in the outer catch block will catch
it as well. It should not show up in both places.

For example
try 'Outer
'Do stuff
'want to catch an exception on a specific action
try
Throw new Exception("test")
catch ex as Exception
RedirectPage("Error.aspx")
End Try

catch tex as TreadAbortException
'This should never fire as the RedirectPage function catches it. But
it's showing up after the RedirectPage catches a ThreadAbortException
already
catch ex as exception
'should be only something not handled previously.
End Try


Public sub RedirectPage(url as string)
Dim strMsg As String
Try
Response.Redirect(url, TRUE)
Catch tex As Threading.ThreadAbortException ' this is SUPPOSED to
catch the threadAbort if true is passed in, but it doesnt always
strMsg = tex.Message
Catch ex As Exception
strMsg = ex.Message
End Try

End Sub
 
G

Guest

I am not surprised you are bubbling up Response.Redirect errors. There is an
easy way around it.

In your catches set some booleans instead of Response.Redirect. You can then
create an if block to determine where to send the person:

If (pageInError) Then
Response.Redirect("Error.aspx")
Else If ....

etc.

Do not have any work after this block. You should have already answered the
question of whether you are redirecting and where by this time.

Why the error? It is simple. You are sending out a Response.Redirect to the
client, which makes the client request the new page. Any code after the
redirect should not be run, so the thread answering THIS PARTICULAR request
has to abort. If the last action is the Redirect, you get around this issue.
It is all in the ordering.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top