1

Windows FormsVisual Studio 2012、C#、.NET 4.5 を使用してアプリケーションを作成しています。Windows 7。

未処理の例外を独自のエラー ハンドラで処理したいと考えています。以下に示すようなコードをいくつか書きました。エラー処理の 2 つの異なる方法も試しました。

このプログラムを Visual Studio (2012) 内で実行すると、非常にうまく動作します。

しかし、エクスプローラーからプログラムを起動すると、エラー ハンドラーが呼び出されません。代わりに、.NET Framework からの標準エラー ハンドラが常にポップアップ表示されます。

それで、私は何を間違っていますか?

static void Main()
{
    try
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleUnhandledException);
        Application.Run(new frmMainFrame());
    }
    catch (Exception ex)
    {
        // Here is my error handler ....
    }
}

static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args) 
{
    Exception ex = (Exception) args.ExceptionObject;
    // Here is my error handler ....
}
4

2 に答える 2

1

Application.ThreadExceptionも接続します

または、Application.SetUnhandledExceptionModeUnhandledExceptionMode.ThrowExceptionに設定して、appdomain ハンドラーを開始します。

于 2013-08-04T12:54:53.650 に答える
0

まず、Debugger.Break()メソッドを使用して、プログラムを問題のあるコード部分にアタッチします。currentDomain が null でないかどうかを確認するだけです。CLI を強制的に停止する例外を発生させないと仮定します。

于 2013-08-04T12:55:28.247 に答える