c# – Control.Invoke展开外部异常并传播内部异常
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Control.Invoke展开外部异常并传播内部异常,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1542字,纯文字阅读大概需要3分钟。
内容图文
![c# – Control.Invoke展开外部异常并传播内部异常](/upload/InfoBanner/zyjiaocheng/824/0f6de7ec7e634ef38c801f0e1aecf569.jpg)
下面的MessageBox.Show调用显示“内部”.这是一个错误吗?
private void Throw()
{
Invoke(new Action(() =>
{
throw new Exception("Outer", new Exception("Inner"));
}));
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Throw();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); // Shows "Inner"
}
}
解决方法:
我看了一下System.Windows.Forms.Control的参考源,处理Invoke的代码如下所示:
try {
InvokeMarshaledCallback(current);
}
catch (Exception t) {
current.exception = t.GetBaseException();
}
GetBaseException:
public virtual Exception GetBaseException()
{
Exception inner = InnerException;
Exception back = this;
while (inner != null) {
back = inner;
inner = inner.InnerException;
}
return back;
}
显然它就像设计一样.来源中的评论没有解释为什么他们这样做.
编辑:一些现在已经消失的网站声称这条评论来自微软的一个人:
Based on the winform comfirmation in the record, our analysis is
correct of the root cause and this behavior is intended. The reason was to
prevent the user from seeing too much of the Windows.Forms internal mechanisms.
This is because the winform’s default error dialog also leverages Application.ThreadException to show the exception details. .Net Winform
team trims the other exceptions information so that the default error
dialog will not display all the details to the end user.Also, some MSFTs have sugguested to change this behavior. However, .Net
Winform team thinks that changing the exception to throw is a breaking
change and for this reason WinForms will keep sending the innermost exception to the Application.ThreadException handler.
内容总结
以上是互联网集市为您收集整理的c# – Control.Invoke展开外部异常并传播内部异常全部内容,希望文章能够帮你解决c# – Control.Invoke展开外部异常并传播内部异常所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。