【发布时间】:2013-09-12 07:18:06
【问题描述】:
我得到以下代码中的ServiceRuntimeException is unhandled by user code,其中ServiceRuntimeException是自定义异常,如何解决?
List<Action> a=new List<Action>();
try
{
foreach (var parallelActivity in parallelActivities)
{
a.Add(delegate
{
try
{
parallelActivity.Execute();
}catch(ServiceRuntimeException e)
{
throw e;//<--Exception thrown at this line
}
});
}
Parallel.Invoke(a.ToArray());
}
catch (ServiceRuntimeException e)
{
throw e;
}catch(AggregateException e)
{
throw e.InnerExceptions[0];
}
ServiceRuntimeException的定义
public sealed class ServiceRuntimeException : Exception
{
public ServiceRuntimeException(string msg)
: base(msg)
{
}
}
【问题讨论】:
-
捕获后不要再通过变量名抛出异常。您将丢失堆栈跟踪!如果你抓住了一些东西并想重新扔它,请使用 throw;没有变量。或者一开始就不要抓住它。
-
@nvoigt,感谢使用 throw 的建议,学习新事物。但这并不能解决这里的问题......