【发布时间】:2012-02-24 19:40:10
【问题描述】:
(最初的内容被删掉了,因为我发现它无关紧要)
我的应用程序有一个通用的 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 );
}
}
当“testing stack trace 6”异常被抛出时,当被catch(WebException wex)捕获时,wex的堆栈跟踪只包含一个条目。
这是为什么?
【问题讨论】:
-
异常代码实际上是什么样的?您是在使用
throw ex还是只是throw将异常传递到链中? -
你的线程的入口点是什么方法?是
DoAsync(Action)吗?您能否展示创建、启动和执行异步任务的代码? -
您没有发布缺少的中间方法的代码,因此很难猜测。这里至少有两种情况。您只会看到任务执行的方法,异常跟踪是基于线程的。抖动优化器会为内联的小方法生成代码,因此您看不到这些,异常是基于堆栈帧的。使用 [MethodImpl] 抑制该优化。
-
对不起。在做了一些研究以缩小问题范围后,我已经完全重写了这个问题。
标签: .net winforms generics exception-handling