我正在做一个相当大的项目,它不太可能抓住一切.我发现事件通知我未处理的异常,但我还没有找到一种方法以编程方式关闭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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1723 次 |
最近记录: |