我正在做一个相当大的项目,它不太可能捕获所有内容。我发现了通知我未处理异常的事件,但是我还没有找到以编程方式关闭 Windows 错误对话框的方法。理想情况下,如果存在未处理的异常,我希望触发该事件,提供一个对话框告诉用户存在问题,然后正常关闭。有什么办法可以做到这一点吗?我意识到我可以将最高层包裹在 try catch 中,但我希望有一些更优雅的东西。
这就是我们所做的。
static void Main() {
try
{
SubMain();
}
catch (Exception e)
{
HandleUnhandledException(e);
}
}
private static void SubMain()
{
// Setup unhandled exception handlers
AppDomain.CurrentDomain.UnhandledException += // CLR
new UnhandledExceptionEventHandler(OnUnhandledException);
Application.ThreadException += // Windows Forms
new System.Threading.ThreadExceptionEventHandler(
OnGuiUnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
// CLR unhandled exception
private static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
HandleUnhandledException(e.ExceptionObject);
}
// Windows Forms unhandled exception
private static void OnGuiUnhandledException(Object sender,
System.Threading.ThreadExceptionEventArgs e)
{
HandleUnhandledException(e.Exception);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)