1
我有以下代碼來運行一些異步調用。我正在嘗試捕獲數據庫超時異常並顯示一些用戶友好的消息。如何在Page.RegisterAsyncTask方法中使用try catch塊
但是把try, catch block in endInvoke, action
沒有執行。我使用ASP.Net 4.0 & C#4.0
Page.RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(beginMyMethod)
, new EndEventHandler(endMyMethod), new EndEventHandler(timeout => { })
, true, true));
Page.ExecuteRegisteredAsyncTasks();
然後
IAsyncResult beginMyMethod(object sender, EventArgs e, AsyncCallback cb, object state)
{
Action r = myMethod;
return r.BeginInvoke(cb, state);
}
void endMyMethod(IAsyncResult asyncResult)
{
try{
myObj.Property // gives object reference error, as it was not set, since DB timeout
}
catch(MyTimeoutException ex){
//it is not getting called
}
}
private void MyMethod()
{
try{
MyObject myObj= //making a database call causes DB time out error
}
catch(MyTimeoutException ex){
//it is getting called
}
}
如何Page.RegisterAsyncTask方法使用try catch塊
注:我想阻止對象引用錯誤,db超時和異步操作
你說得對。我能夠以原始方法捕捉異常。但是,我需要在訪問'endXXX'之前檢查空值。 – Billa 2014-09-01 11:51:42
那你爲什麼不去做呢? – usr 2014-09-01 11:57:06
爲什麼不嘗試捕捉多次(** catch(MyTimeOutException)**和** catch(NullReferenceException)**?) – jbutler483 2014-09-01 11:58:46