- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
似乎在属性的 Set 方法中发生的异常不会冒泡到应用程序的 ThreadException 事件。
我们将该事件与 AppDomain.CurrentDomain.UnhandledException 事件一起使用,以捕获应用程序中发生的任何意外事故。异常详细信息将写入日志,以便我们的支持和开发团队可以更好地评估问题。遗憾的是,在这种特殊情况下,Catch All 似乎达不到要求。
StackOverflow上有几个类似的问题,但是没有回答解决全局异常处理不捕获异常的问题。我已经知道我们可以修复它,所以不会发生异常。我们可以为每个 setter 添加一个 TryCatch block 。我们可以将 BindingComplete 事件添加到每个数据绑定(bind)并以这种方式获取异常。但所有这些都违背了全局异常处理的目的,而全局异常处理在任何其他情况下都能完美运行。
要重现该问题,只需创建一个带有文本框的表单,将文本框绑定(bind)到一个属性并在该属性的 set 方法中抛出异常。将 ThreadException 和 UnhandledException 事件添加到 program.cs。运行程序并在文本框中键入以触发异常。调试器将中断异常,按继续 (F5) 让异常像在调试器之外一样冒泡。任何正常的异常都会在这些事件中结束,但这个不会。
Form1.cs
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.DataBindings.Add("Text", this, "TestValue", true, DataSourceUpdateMode.OnPropertyChanged);
}
private string _TestValue = "";
public string TestValue
{
get{return _TestValue;}
set
{
_TestValue = value;
throw new Exception("Something bad happened in here");
}
}
Program.cs
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += ThreadExceptionHandler;
AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
Application.Run(new Form1());
}
private static void ThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs args)
{
try
{
//RR.Common.ErrorLogRt.WriteError(args.Exception.StackTrace.ToString(), args.Exception.Message.ToString(), true);
MessageBox.Show(args.Exception.Message);
}
catch
{
MessageBox.Show("Error writing to exception log. This program will now terminate abnormally.");
Application.Exit();
}
}
static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
{
try
{
if (e != null)
{
Exception ex = e.ExceptionObject as Exception;
//RR.Common.ErrorLogRt.WriteError(ex.StackTrace.ToString(), ex.Message.ToString(), true);
MessageBox.Show(ex.Message);
}
else
{
MessageBox.Show("Unhandled Error: " + e.ToString());
}
}
catch
{
MessageBox.Show("Error writing to exception log. This program will now terminate abnormally.");
Application.Exit();
}
}
static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
try
{
if (e != null && e.Exception != null && e.Exception.InnerException != null)
{
//The unobserved exception is always the same, The actual exception that cause it will be the inner exception.
Exception ex = e.Exception.InnerException;
MessageBox.Show(e.Exception.Message);
//RR.Common.ErrorLogRt.WriteError(ex.StackTrace.ToString(), ex.Message.ToString(), true);
}
else
{
MessageBox.Show("Unhandled Error: " + e.ToString());
}
}
catch
{
MessageBox.Show("Error writing to exception log. This program will now terminate abnormally.");
Application.Exit();
}
}
}
最佳答案
Remarks from Binding.FormattingEnabled Property
Setting this property to true also enables error-handling behavior and causes the BindingComplete event to be raised. The handler of this event can take the appropriate action, based on the success, error, or exceptions in the binding process, by examining the BindingCompleteState property of the BindingCompleteEventArgs parameter.
internal bool PushData(bool force)
{
Exception ex = null;
if (!force && this.ControlUpdateMode == ControlUpdateMode.Never)
{
return false;
}
if (this.inPushOrPull && this.formattingEnabled)
{
return false;
}
this.inPushOrPull = true;
try
{
if (this.IsBinding)
{
object value = this.bindToObject.GetValue();
object propValue = this.FormatObject(value);
this.SetPropValue(propValue);
this.modified = false;
}
else
{
this.SetPropValue(null);
}
}
catch (Exception ex2)
{
ex = ex2;
if (!this.FormattingEnabled)
{
throw;
}
}
finally
{
this.inPushOrPull = false;
}
if (this.FormattingEnabled)
{
BindingCompleteEventArgs bindingCompleteEventArgs = this.CreateBindingCompleteEventArgs(BindingCompleteContext.ControlUpdate, ex);
this.OnBindingComplete(bindingCompleteEventArgs);
return bindingCompleteEventArgs.Cancel;
}
return false;
}
如您所见,将第 4 个参数传递为 true:DataBindings.Add("Text", this, "TestValue", true
负责捕获 PushData
中的异常> 并将其传递给 BindingComplete
事件。除了 BindingComplete
之外,没有其他方法(AppDomain.CurrentDomain.FirstChanceException
除外)可以找到异常> 如果启用格式。
关于c# - 绑定(bind)属性的 Set 方法中的异常未被 Application.ThreadException 事件捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24702330/
当抛出异常并在 Application.ThreadException 事件处理程序中捕获它们时,我看到了一些奇怪的行为。 基本上下面示例中发生的事情是在 BackgroundWorker 的 DoW
当抛出异常并在 Application.ThreadException 事件处理程序中捕获它们时,我看到了一些奇怪的行为。 基本上下面示例中发生的事情是在 BackgroundWorker 的 DoW
我刚刚在 .NET 2.0 应用程序中修复异常处理,我偶然发现了一些奇怪的问题 Application.ThreadException . 我想要的是能够从 GUI 元素(例如 button_Clic
我最近在我的C#(WinForms)应用程序中实现了一些(基本)未处理的错误处理程序。 UnhandledExceptionEventHandler工作正常,但是在ThreadExceptionEve
我有一个来自应用程序的窗口,带有这样的 program.cs 代码: static class Program { /// /// The main entry point for
我们有一个很大的应用程序,为了确保我们的客户不会收到“XXX 已停止工作”的消息,我们添加了一些处理程序(我们在其中记录了一些错误和一些其他内容)。 它工作正常,我们已经解决了很多这样的问题(通过分析
我正在使用 Application.ThreadException在我的 winforms 应用程序中处理和记录意外异常的事件。 现在,在我的应用程序中的某个地方,我得到了以下代码(或者更确切地说,是
Application.ThreadException的reference page说 Because this is a static event, you must detach your eve
我有一个方法,可以从网络获取Object,然后使用此数据更新UI。 我使用第三方API来获取数据并接收数据。下载过程在新的 Thread 中完成,获取和更新数据在 UI Thread 中完成。 在我的
首先是一些背景知识:我有一个多线程 WinForms 应用程序,它正在与 native dll 进行互操作。此应用程序有时会因未处理的异常而崩溃,我们正在尝试调查为什么会发生这种情况。为了方便它,我正
我不明白为什么在 VS2010 中使用 (CTRL-F5 - Run without Debugger) 在以下情况下只得到 1 个消息框: Public Class Form1 Privat
我有一个 WinForms 应用程序,它创建了多个表单,每个表单都在自己的 GUI 线程(而不是主 GUI 线程)中。我想处理所有这些表单的未处理异常事件 (Application.ThreadExc
好吧,这是一个简单的: Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException 有什么区别>? 我需要同时处理
似乎在属性的 Set 方法中发生的异常不会冒泡到应用程序的 ThreadException 事件。 我们将该事件与 AppDomain.CurrentDomain.UnhandledException
我遇到了一个应用程序挂起的问题,并给我默认的“请告诉微软这个问题”弹出窗口,而不是应用程序中的“未处理的异常”对话框。 在应用程序代码中,Application.ThreadException 和 A
在我使用的 WinForms 中: System.Windows.Forms.Application.ThreadException System.Windows.Application.Unhand
是否有任何理由更喜欢在 Windows 窗体应用程序中实现全局异常处理程序的这些方法中的一种而不是另一种? 第一种方法 static void Main(string[] args) { tr
我是一名优秀的程序员,十分优秀!