通过反射调用方法时,异常未处理

时间:2013-04-30 17:47:26

标签: c# reflection delegates unhandled-exception

使用委托,我正在调用一个返回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");
}

Visual Studio错误:

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");
}

我希望错误冒泡到“Main”,因为那是我的顶级try / catch,但事实并非如此。相反,我在“ExecuteConfiguration”中得到“ApplicationExcpetion未处理”。

1 个答案:

答案 0 :(得分:3)

你正在捕捉然后立即再扔它。你应该妥善处理它。

try {
    Func<int> processElement = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), o, "ProcessElement");
    result = processElement();
} catch (Exception) {
    //ignore the exception (not recomended)
}