(由于我发现原始内容无关紧要而被删除了原始内容)
我的应用程序有一个通用的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”异常时,当catch捕获时(WebException wex),wex的堆栈跟踪只包含一个条目。
为什么会这样?
答案 0 :(得分:0)
看起来您将原始异常包装到自定义异常类中。当您再次读取异常以查看堆栈跟踪时,您还必须检查内部异常行以获取完整的跟踪。
编辑 -
堆栈跟踪从抛出异常的地方开始,因此显示的堆栈跟踪是正确的,我对问题的原始描述是正确的。
无论何时抛出新异常,该异常的堆栈跟踪都会从该点开始。
再次在原始示例中,检查此处的内部异常:
//throwing the exception resets the stack trace!
//the next catch will see the exception as coming from this line, and
//will need to look at the inner exception at that point to see the
//original exception! I Think! I have no idea what RestException is,
//only that your passing the original exception ex into it as a parameter.
throw new RestException("Non-WebException during GetResponse.", txn, ex );
答案 1 :(得分:0)
如果你的一个(未示出的)方法正在捕获异常,然后像这样重新抛出它:
throw ex;
然后堆栈跟踪将被重置为从重新生成点开始。
throw;
将保持堆栈跟踪。