2

我有这个代码

protected void Button_Click(object sender, EventArgs e)
{
    try
    {
        // some code

        con.Open();
        string result = command.ExecuteScalar().ToString();

        if (result != string.Empty)
        {
             // some code
             Response.Redirect("Default.aspx");
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        con.Close();
    }

它给出了一个例外Response.Redirect("Default.aspx");

例如:线程被中止。

知道为什么吗?

谢谢

4

2 回答 2

2

从 Try...Catch 语句中重定向将导致抛出此异常,因此这不是您想要做的。

我会将您的代码更新为;

string result = string.Empty;

try
{
    // some code
    con.Open();
    result =  command.ExecuteScalar().ToString();        
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}
finally
{
    con.Close();
}

if (result != string.Empty)
{
     // some code
     Response.Redirect("Default.aspx");
}
于 2013-03-14T16:26:54.217 回答
0

这是 ASP.NET 在执行重定向时抛出的典型异常。它在 Internet 上有很好的记录。

尝试下面的 catch 块来吞下异常,一切都应该没问题。它应该什么都不做!

catch(ThreadAbortException)
{
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}
finally
{
    con.Close();
}
于 2013-03-14T16:27:52.403 回答