(初始內容編輯,因爲我發現這是無關緊要的).NET - 異常堆棧跟蹤線路上通用的方法,消除
我的應用程序有它包裝在 委託業務,並以往通用的RESTful HTTP客戶端我的WrapOperation方法(如下所示)。
然而,當異常被拋出堆棧跟蹤只包含一個條目:
at MyProject.Client.RestClient.WrapOperation[T](String method, String path, Object requestObject, RestOperation`1 action) in D:\{fileName}\RestClient.cs:line 196
我已經減少了問題的代碼,只是這樣的:
private T WrapOperation<T>(String method, String path, Object requestObject, RestOperation<T> action) {
HttpWebRequest request;
RestTransaction txn = CreateRequest(method, path, requestObject, out request);
////////////////////////////
try {
throw new WebException("testing stack trace 6");
throw new Exception("testing stack trace 7");
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
txn.GotResponse(response);
return action(txn, response);
}
} catch(WebException wex) {
// NOTE: When exceptions are caught, they're missing the first few entries of the stack trace
// And appear as though "WrapOperation[T]" (sic) is the entrypoint. Why is this?
if(wex.Response != null) {
HttpWebResponse response = (HttpWebResponse)wex.Response;
txn.GotResponse(wex, response);
CopyResponseToMemoryStream(response, txn).Dispose();
} else {
txn.GotResponse(wex);
}
// NOTE: However, when these RestExceptions are caught (by WrapOperation's caller), their stack trace is complete and shows the entire trace
throw new RestException("WebExeption during GetResponse.", txn, wex);
} catch(Exception ex) {
txn.GotResponse(ex);
// NOTE: However, when these RestExceptions are caught (by WrapOperation's caller), their stack trace is complete and shows the entire trace
throw new RestException("Non-WebException during GetResponse.", txn, ex);
}
}
當「測試堆棧跟蹤6 「拋出異常時,被捕獲(WebException wex)捕獲時,wex的堆棧跟蹤僅包含一個條目。
這是爲什麼?
異常代碼的外觀如何?你正在做'拋出'還是'拋出'來傳遞鏈中的異常? – asawyer 2012-02-24 19:41:50
你沒有發佈缺少的中間方法的代碼,所以很難猜測。這裏至少有兩種情況。你只會看到任務執行的方法,異常跟蹤是基於線程的。抖動優化器爲內聯小方法生成代碼,這樣您就無法看到這些代碼,異常是基於堆棧幀的。使用[MethodImpl]來抑制該優化。 – 2012-02-25 14:28:11
什麼方法是你的線程的入口點?它是
DoAsync(Action)
?你能否展示創建,啓動和執行異步任務的代碼? – 2012-02-25 11:59:46