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

Discussion in 'ASP .Net' started by Eric, Aug 19, 2005.

  1. Eric

    Eric Guest

    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
    Eric, Aug 19, 2005
    #1
    1. Advertising

  2. RE: Response.Redirect gens ThreadAbortException that's either not bein

    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!
    ***************************


    "Eric" wrote:

    > 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
    >
    >
    >
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Aug 19, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    5
    Views:
    791
    Herfried K. Wagner [MVP]
    Sep 19, 2003
  2. VB Programmer
    Replies:
    1
    Views:
    1,716
    David Browne
    Nov 6, 2003
  3. Henrik Stidsen
    Replies:
    5
    Views:
    22,823
    Henrik Stidsen
    Sep 1, 2006
  4. Sal
    Replies:
    1
    Views:
    403
  5. Martin Plechsmid

    Exceptions thrown in includes not caught

    Martin Plechsmid, Jul 7, 2003, in forum: ASP General
    Replies:
    9
    Views:
    120
    Evertjan.
    Aug 3, 2003
Loading...

Share This Page