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



使用委托,我调用一个对象(" 0 ")方法("ProcessElement"),它返回一个int:

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 Error:

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,但它没有。相反,我在"executecconfiguration"中得到"applicationexception was unhandled"。

你接住了,然后马上又扔了一次。你应该适当地处理它。

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

相关内容

  • 没有找到相关文章

最新更新