我試圖設置TimeoutException for命令,如果在代碼中出現「Command TimeoutException」,它必須進入catch塊並進一步重定向到customerror.aspx頁面。 代碼是:如何在C#中設置TimeoutException並將其重定向到CustomErrorPage.aspx
try
{
command.Timeout=1; //which is in seconds for testing i have set it to one.
//some code here
// if TimeoutException occurs, it must go to following block which doesnthappen
//And it goes to catch 2: block..! Why is it so...?
}
//catch 1:
catch (TimeoutException ex)
{
Response.Redirect("CustomError.aspx");
}
//catch 2:
catch (Exception ex)
{
Response.Redirect("CustomError.aspx");
}
像上面這樣的嵌套異常,當其中一個被緩存時,其餘的被忽略更多,就像if .. else if – 2012-01-04 19:08:27
ya我同意......!但是,爲什麼不抓住1塊代碼,何時會發生TimeoutException?當我將它設置爲1秒時,它必須去TimeoutException但它失敗......爲什麼? :-( – 2012-01-04 19:17:12
因爲你實際得到的異常是不是一個TimeoutException - 可能是某種類型的特定於數據庫的異常 – Leon 2012-01-04 19:40:01