C# Response.Redirect引起的錯誤


今天遇到一個問題,代碼格式如下:

try
{
Response.Redirect( " index.aspx ");
}
catch(Exception ex)
{
Response.Write( " 錯誤: " + ex.ToString());
}

這里總是捕捉到錯誤:

System.Threading.ThreadAbortException: 正在中止線程。
   在 System.Threading.Thread.AbortInternal()
   在 System.Threading.Thread.Abort(Object stateInfo)
   在 System.Web.HttpResponse.End()
   在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
   在 System.Web.HttpResponse.Redirect(String url)
原來Response.Redirect會執行httpResponse.End() ,從來引起中止線程的錯誤,修改如下: 
catch (System.Threading.ThreadAbortException e)
{
// 這個異常不需要特別處理
Debug.Write( " ...... ");
}
catch (SqlException err2)
{
。。。
}

加兩上兩層錯誤捕捉,System.Threading.ThreadAbortException e 這個不用處理,只加在其它的出錯上即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM