문제

Using a delegate, I'm calling an objects ("o") method ("ProcessElement") which returns an 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;
    }
}

This works great until I encounter an exception.

When I encounter an exception, Visual Studio throws an error stating that the exception was unhandled. I have a try/catch surrounding the delegate. Should this not catch the exception?

ProcessElement method:

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

Visual Studio Error:

ApplicationExcpetion was unhandled

Thanks,

EDIT: Perhaps I'm not beging totally clear- Sorry. I know you can't copy and paste to test, but at any rate, here is more of the story:

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

I would expect the error to bubble up to "Main", because that is my top level try/catch, but it does not. Instead, I get "ApplicationExcpetion was unhandled" in "ExecuteConfiguration".

도움이 되었습니까?

해결책

You are catching then immediately throwing it again. You should handle it appropriately.

try {
    Func<int> processElement = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), o, "ProcessElement");
    result = processElement();
} catch (Exception) {
    //ignore the exception (not recomended)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top