0

デリゲートを使用して、int を返すオブジェクト ("o") メソッド ("ProcessElement") を呼び出しています。

int result;
object o;
...
if (o != null) {
    try {
        Func<int> processElement = 
            (Func<int>)Delegate.CreateDelegate(
                typeof(Func<int>), 
                o, 
                "ProcessElement");
        result = processElement();
    } catch (Exception) {
        throw;
    }
}

例外が発生するまで、これはうまく機能します。

例外が発生すると、Visual Studio は例外が処理されなかったことを示すエラーをスローします。デリゲートの周りに try/catch があります。これは例外をキャッチすべきではありませんか?

ProcessElement メソッド:

public int ProcessElement () {
    throw new ApplicationException("test");
}

ビジュアル スタジオ エラー:

ApplicationExcpetion was unhandled

ありがとう、

編集:おそらく私は完全にはっきりしていません-申し訳ありません。コピーして貼り付けてテストすることはできないことはわかっていますが、いずれにせよ、詳細は次のとおりです。

static void Main (string[] args) {
    try {
        Thread processThread = new Thread(ExecuteConfiguration);
        processThread.Start();
        if (!processThread.Join(TimeSpan.FromMilliseconds(int.Parse((_Configuration2.TimeOutMinutes * 60 * 1000).ToString())))) {
            processThread.Abort();
            Status = Program.BatchJobsStatus.TimeOut;
            throw new ApplicationException("Time out");
        }
    } catch (Exception ex) {
        //Expecting to catch error thrown in ExecuteConfiguration -> SendEmail here.
        Logger.WriteErrorMessage("\n" + ex.ToString());
    }
}

private static void ExecuteConfiguration () {
    for (int i = 0; i < ElementCount; i++) {
        if (ContinueProcessing) {
            object o = GetConfigurationElementById(_Configuration2, "ElementId", i.ToString());
            if (o != null) {
                MethodInfo method = o.GetType().GetMethod("ProcessElement");
                if (method != null) {
                    try {
                        Console.WriteLine("(ElementId " + i.ToString() + ") " + o.GetType().FullName);
                        Func<int> processElement = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), o, method);
                        i = processElement();
                    } catch (Exception) {
                        // rethrow error thrown in SendEmail
                        throw;
                    }
                } else {
                    Console.WriteLine("(ElementId " + i.ToString() + ") " + o.GetType().FullName + " method ProcessElement does not exist");
                }
            }
        }
    }
}

public int ProcessElement () {
    int result = ElementId;
    SendEmail(); // error thrown here, resulting in "Exception was unhandled" error in calling method (ExecuteConfiguration)
    for (int i = ElementId + 1; i < Program.ElementCount; i++) {
        if (Program.ContinueProcessing) {
            object o = Program.GetConfigurationElementById(this, "ElementId", i.ToString());
            if (o != null) {
                MethodInfo method = o.GetType().GetMethod("ProcessElement");
                if (method != null) {
                    try {
                        Console.WriteLine("(ElementId " + i.ToString() + ") " + o.GetType().FullName);
                        Func<int> processElement = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), o, method);
                        result = processElement();
                    } catch (Exception) {
                        throw;
                    }
                } else {
                    Console.WriteLine("(ElementId " + i.ToString() + ") " + o.GetType().FullName + " method ProcessElement does not exist");
                    result = i;
                }
            }
        }
    }
    return result;
}

public void SendEmail() {
    throw new ApplicationException("test");
}

それが私の最上位のtry/catchであるため、エラーが「メイン」にバブルアップすることを期待していますが、そうではありません。代わりに、「ExecuteConfiguration」で「ApplicationExcpetion was unhandled」が表示されます。

4

1 に答える 1

3

あなたはキャッチしてすぐにもう一度投げます。適切に処理する必要があります。

try {
    Func<int> processElement = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), o, "ProcessElement");
    result = processElement();
} catch (Exception) {
    //ignore the exception (not recomended)
}
于 2013-04-30T17:50:17.670 に答える