[英]Exception was unhandled when calling method via reflection
使用委托,我正在調用一個對象(“ o”)方法(“ 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會引發錯誤,指出未處理該異常。 我對代表有一個嘗試/抓住。 這應該不能捕捉到異常嗎?
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”,因為這是我的最高嘗試/捕獲,但事實並非如此。 相反,我在“ ExecuteConfiguration”中得到“未處理ApplicationExcpetion”。
您正在捕捉,然后立即將其再次拋出。 您應該適當地處理它。
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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.