IAd*_*ter 3 .net c# error-handling catch-all
我试过用
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399
但是当我这样做的时候
throw new ArgumentNullException("playlist is empty");
Run Code Online (Sandbox Code Playgroud)
我一无所获.我敢打赌,我错过了一些非常明显的东西.
这是我的代码.
using System;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Threading;
namespace MediaPlayer.NET
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的代码工作得很好,没有很多我可以想到的失败模式.除了一个之外,当您在Windows 8之前的64位操作系统上调试32位代码时,调试器和Windows SEH之间的交互存在问题.这可能导致异常在表单中出现时没有任何诊断被吞下加载事件或OnLoad()方法覆盖.检查链接的帖子以获取解决方法,最简单的一个是Project + Properties,Build选项卡,Platform Target = AnyCPU,如果你看到它,请取消选中"Prefer 32-bit".
通常,通过不让Application.ThreadException的默认异常处理显示对话框,您正在做适当的事情.但保持简单,这样做:
#if (!DEBUG)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
#endif
Run Code Online (Sandbox Code Playgroud)
现在您再也不用担心ThreadException,所有异常都会触发AppDomain.UnhandledException事件处理程序.并且代码周围的#if仍允许您调试未处理的异常,调试器将在引发异常时自动停止.
将此添加到UnhandledException方法以防止Windows崩溃通知显示:
Environment.Exit(1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5959 次 |
| 最近记录: |