由Response.Redirect引发的"Thread was being aborted. "异常的处理方法

将Response.Redirect写入try...catch会出现异常
try 

    Response.Redirect(
"Index.aspx"); 

catch(Exception e) 

    Response.Redirect(
"Error.aspx?message=" + e.Message); 

如上,则会显示"Thread was being aborted. "异常

同样的还有Response.End()等提前结束当前Theard的方法

解决方案一:

如果 Response.Redirect("Index.aspx"); 必须写到Try{}里,则可以在Catch语句写入:

catch(Exception e) 

    
if (!(e is ThreadAbortException)) 
    { 
        Response.Redirect(
"Error.aspx?message=" + e.Message); 
    } 
}
  略过系统对这个特殊异常的处理。

 解决方案二:

MSDN已经解析清楚了
“调用 Redirect 等效于在将第二个参数设置为 true 的情况下调用 Redirect。 Redirect 调用 End,它在完成时引发 ThreadAbortException 异常。” 可见Redirect方法在内部是调用 Thread.Abort()来中止线程的从而引发ThreadAbortException 异常。如果不想立刻中止则,第二个参数设置为false

C# code

protected void Button1_Click(object sender, EventArgs e)
{
try
{
Response.Redirect(
"A.aspx",false);
}
catch( Exception e1)
{
Response.Redirect(
"B.aspx");
}
}

 From:

http://topic.csdn.net/u/20080909/10/fa572c74-5f5b-4996-a649-36691fcfadcb.html

http://www.zxbc.cn/html/20071128/29848.html

posted @ 2008-11-16 12:18  emanlee  阅读(2517)  评论(0编辑  收藏  举报