2

エラーレポートを出力し、ユーザーがそのレポートを私に送信するかどうかを尋ねる ErrorRecorder アプリがあります。

次に、メインアプリがあります。エラーが発生した場合、エラー レポートをファイルに書き込み、そのファイルを開いてユーザーにエラー レポートを表示するように ErrorRecorder に要求します。

したがって、Try/Catch を使用してほとんどのエラーをキャッチしています。

しかし、まったく予期しないエラーが発生し、プログラムがシャットダウンした場合はどうなるでしょうか。

Global/Override メソッドなど、プログラムに「予期しないエラーが発生した場合はシャットダウンする前に、「ErrorRecorderView()」メソッドを呼び出す」ように指示するようなものがありますか?

4

1 に答える 1

5

私はこれがあなたが求めているものだと思います-アプリケーションドメインレベルで、つまりプログラム全体で例外を処理できます。
http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx

using System;
using System.Security.Permissions;

public class Test
{

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Example()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

    try
    {
        throw new Exception("1");
    }
    catch (Exception e)
    {
        Console.WriteLine("Catch clause caught : " + e.Message);
    }

    throw new Exception("2");

    // Output: 
    //   Catch clause caught : 1 
    //   MyHandler caught : 2
}

static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    Console.WriteLine("MyHandler caught : " + e.Message);
}

public static void Main()
{
    Example();
}

}

于 2013-02-08T13:32:11.463 に答える