什么是展示我的全部的正确方法InnerException.
我发现我的一些InnerExceptions有另一个InnerException,而且非常深入.
会InnerException.ToString()做的工作,我还是我通过需要循环InnerExceptions,建立一个String有StringBuilder?
我已阅读MSDN但我无法理解这个概念.
如果我错了,纠正我,
当前异常将使用innerexception.
首先发生内部异常,然后发生当前异常(如果有异常),这InnerException就是检查的原因null.为了保留内部异常,我们必须将其作为参数传递.
我对吗?
我有许多方法正在调用Delegate.DynamicInvoke.其中一些方法会进行数据库调用,我希望能够捕获一个SqlException而不是TargetInvocationException通过它的内部捕获并查找实际出错的方法.
我正在使用此方法重新抛出但它清除了堆栈跟踪:
try
{
return myDelegate.DynamicInvoke(args);
}
catch(TargetInvocationException ex)
{
Func<TargetInvocationException, Exception> getInner = null;
getInner =
delegate(TargetInvocationException e)
{
if (e.InnerException is TargetInvocationException)
return getInner((TargetInvocationException) e.InnerException);
return e.InnerException;
};
Exception inner = getInner(ex);
inner.PreserveStackTrace();
throw inner;
}
Run Code Online (Sandbox Code Playgroud)
这个PreserveStackTrace方法是我通过另一个帖子修复的扩展方法(我不知道它实际上做了什么).但是,这似乎不会保留跟踪:
public static void PreserveStackTrace(this Exception e)
{
var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
var mgr = new ObjectManager(null, ctx);
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
e.GetObjectData(si, ctx);
mgr.RegisterObject(e, 1, si);
mgr.DoFixups();
}
Run Code Online (Sandbox Code Playgroud) c# exception-handling stack-trace targetinvocationexception inner-exception
我正在为WCF Web服务编写单元测试.我故意向服务器发送无效请求,抛出一个WebExceptionFault<string>.在单元测试中,被抓住的异常是EndpointNotFoundException与标准WebException的InnerException属性.我想验证响应的主体是否匹配我认为应该存在的字符串.
然而,我遇到了一个问题,因为(这是一个)的Response属性是Disposed并且无法读取.WebExceptionSystem.Net.SyncMemoryStream
我的代码:
Clubs actual;
try
{
//"201" is an invalid argument for the first parameter
actual = new Clubs(channel.GetClubs("201", "123"));
}
catch (EndpointNotFoundException e)
{
WebException w = e.InnerException as WebException;
Assert.IsNotNull(w);
HttpWebResponse resp = w.Response as HttpWebResponse;
Assert.IsNotNull(resp);
Assert.AreEqual(resp.StatusCode, HttpStatusCode.NotFound);
//Next line throws an ArgumentException saying Stream not readable
//Investigation shows it has been disposed
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
Assert.AreEqual(sr.ReadToEnd(), "League not allowed …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
protected string formatException(Exception e)
{
var exError = "<form>";
if (e == null)
{
throw new ArgumentNullException("e");
}
exError += "<fieldset><legend><a href='#'>" +
"<span class='show-expanded'>collapse message</span>" +
"<span class='show-collapsed'>expand message</span>" +
"</a></legend><p>" + e.Message + "</p></fieldset>";
exError += "<fieldset><legend><a href='#'>" +
"<span class='show-expanded'>collapse trace</span>" +
"<span class='show-collapsed'>expand trace</span>" +
"</a></legend><p>" + e.StackTrace + "</p></fieldset>";
if (e.InnerException != null)
{
// same functionality but for the inner exception and the InnerException.InnerException
}
return exError + "</form>";
}
Run Code Online (Sandbox Code Playgroud)
调用时它会格式化异常消息。不过我想让它包括InnerException和 …