如何使用ThreadException?

我试过用

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399

但是当我这样做的时候

throw new ArgumentNullException("playlist is empty"); 

我一无所获。 我打赌我错过了一些非常明显的东西。

这是我的代码。

 using System; using System.Security.Permissions; using System.Windows.Forms; using System.Threading; namespace MediaPlayer.NET { internal static class Program { ///  /// The main entry point for the application. ///  [STAThread] [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] private static void Main() { // Add the event handler for handling UI thread exceptions to the event. Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); // Set the unhandled exception mode to force all Windows Forms errors to go through // our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // Add the event handler for handling non-UI thread exceptions to the event. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MediaPlayerForm()); } private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) { MessageBox.Show("UnhandledException!!!!"); } private static void UIThreadException(object sender, ThreadExceptionEventArgs t) { MessageBox.Show("UIThreadException!!!!", "UIThreadException!!!!", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); Application.Exit(); } } } 

你的代码工作正常,我想不到很多可能的失败模式。 除了一个,当您在Windows 8之前的64位操作系统上调试32位代码时,调试器和Windows SEH之间的交互存在问题 。这可能导致exception被滥用而没有任何诊断,当它们出现在窗体中时加载事件或OnLoad()方法覆盖。 检查链接的post以获取解决方法,最简单的是Project + Properties,Build选项卡,Platform Target = AnyCPU,如果你看到它,请取消选中“Prefer 32-bit”。

通常,通过不让Application.ThreadException的默认exception处理显示对话框,您正在做适当的事情。 但保持简单,这样做:

 #if (!DEBUG) Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); #endif 

现在您再也不用担心ThreadException,所有exception都会触发AppDomain.UnhandledException事件处理程序。 并且代码周围的#if仍允许您调试未处理的exception,调试器将在引发exception时自动停止。

将此添加到UnhandledException方法以防止Windows崩溃通知显示:

  Environment.Exit(1); 

你没有显示你抛出ArgumentNullException 。 我的猜测是它在MediaPlayerForm的构造函数中(这意味着它在消息循环开始之前)或者它在不同的线程中。 Application.ThreadException仅处理在运行消息循环的Windows窗体线程上捕获的exception。