【发布时间】:2019-03-02 18:23:01
【问题描述】:
在 C# winform 桌面应用程序中,我试图捕获所有可能出现在代码中的异常,我已经尝试过:
using System;
using System.Threading;
using System.Windows.Forms;
namespace Controller
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
}
}
}
我在设备管理器中使用没有串行端口的表单关闭事件附加“COM6”来测试它,但我只看到 Visual Studio An exception of type 'System.InvalidOperationException' occurred in System.dll but was not handled in user code 报告
如何为 winform 应用程序收集错误数据
【问题讨论】:
-
一旦遇到未处理的运行时错误,执行就会停止。除非您报告错误并让它继续运行,否则您不会看到任何其他错误,因为应用程序已停止运行。
标签: c# winforms exception events error-handling