0

クラスで未処理の例外をすべて処理したいのでProgress、エラー ログ用のコードをいくつか書きました。

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoginPoint());
        }
        catch (Exception myException)
        {
            //log the unhandled exceptions.                
        }
    }
}

しかし、の例外はBackgroundWorker正しく処理されていません:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    throw new Exception("TEST EXCEPTION!");
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
    {
        throw new Exception("I GOT THE EXCEPTION");
    }
}

クラスで例外「I GOT ...」を処理したいのですProgressが、アプリケーションを実行 (デバッグではなく) すると、システムの例外ダイアログが表示されます。

4

3 に答える 3

2

AppDomain.UnhandledException イベントを使用できます

于 2013-04-18T14:33:05.613 に答える
1

program.cs で次のようにします。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
    Application.Run(new Form1());
}

static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
    MessageBox.Show(((Exception)e.ExceptionObject).Message, "AppDomain.UnhandledException");
    Environment.Exit(-1);
}

あなたのフォームコードはそれで動作するはずですAppDomain.UnhandledException.Run-Mode(ctrl + F5)でMessageBoxを取得する必要があります.

于 2013-04-18T13:26:03.530 に答える