在asp.net中執行Response.End();
方法時拋出了我在catch塊中處理的ThreadAbortException
。內部catch塊結束後,我想執行一些進一步的代碼,但它直接跳到外部catch塊。這是因爲響應已經結束,.NET框架不執行任何進一步的代碼?執行Response.End()方法時在asp.net中處理ThreadAbortException異常
protected void btn_click(object sender, EventArgs e)
{
try
{
string fileToDownload = MapPath(@"~\Sample.txt");
string fileToRead = MapPath(@"~\FileNotExist.txt");
try
{
//Section 1
try
{
// try to read the file which does not exist to raise the exception
StreamReader ss = new StreamReader(fileToRead);
}
catch (IOException IoEx)
{
// Just for sample exception
}
// Section 2 code block still execute because exception handled by upper try catch block
//Section 2
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=SampleTemplate.txt");
Response.ContentType = "text";
Response.WriteFile(fileToDownload);
Response.Flush();
Response.End();
}
catch (System.Threading.ThreadAbortException abrtEx)
{
// do not treat this exception as Exception
}
//Section 3 Code block not executing even after exception handeled by ThreadAbortException
//Section 3
string test = "Do futher process after sample downloaded";
}
catch (Exception ex) // Outer Catch Block
{
throw ex;
}
}
應該在「下載樣品後進行進一步處理」中做了什麼?該頁面將完成其任務(爲下載文件提供服務,您希望下一步做什麼? – Alexander
@Alexander:我只發佈了我創建的sampel代碼來解釋我的場景,真正的代碼在加載之前做了很多事情,下載currentaly後,我已經做了我們的下載代碼之後,我想糾正和移出catch塊的catch塊。 –
我認爲,儘管如此,但我問的原因是,我害怕你使用的頁面生命週期錯誤,因此,對我來說這是一個重要的問題,對於Page,在結束迴應之後,生活就結束了 – Alexander