Well I didn't know that

Posted by Tom on 2010-09-11 22:24

I like to think I know ASP.NET fairly well, but this is the first time I've come across this.

public void HopefullyRedirect()
{
    try
    {
        SomeComplicatedCrap();
        Response.Redirect("~/SomeplaceElse.aspx");
    }
    catch (Exception e)
    {
        ReportError(e);
    }
}

Yeah, I know - catching Exception is bad. But SomeComplicatedCrap has some complicated crap in it. There were HttpWebRequests and XML parsing and all other kinds of stuff. Seriously. Stop looking at me like that. We've all done it. Jeez.

But that's not the point I'm trying to make. Response.Redirect calls Response.End, which calls Thread.Abort, which bubbles a ThreadAbortException. So now every time that method is called I get an email.

What I should have been doing is this.

public void HopefullyRedirect()
{
    try
    {
        SomeComplicatedCrap();
        Response.Redirect("~/SomeplaceElse.aspx");
    }
    catch (ThreadAbortException)
    {
        throw;
    }
    catch (Exception e)
    {
        ReportError(e);
    }
}

I just find it odd that I've never come across this before. Others things I've discovered recently:

Edumacating!