未処理の例外をキャッチしようとしていると思いますか? このようなもの:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
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");
// here you can log the exception ...
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
// here you can log the exception ...
}
この例では、try-catch セクション (Windows フォーム アプリケーション) でキャッチされていないすべての例外を管理する方法を示します。
UnhandledException イベントは、メイン UI スレッドからスローされたキャッチされていない例外を処理します。ThreadException イベントは、非 UI スレッドからスローされたキャッチされていない例外を処理します。