【问题标题】:Multiple Exceptions have occurred, what's the right way to rethrow?出现多个异常,重新抛出的正确方法是什么?
【发布时间】:2012-02-24 21:12:57
【问题描述】:
public void DoRiskyThings(List<Action> tasks)
{
  List<Exception> exceptions = new List<Exception>();
  foreach(Action task in tasks)
  {
    try {task()}
    catch (Exception e) {exceptions.Add(e)};
  }

  if (exceptions.Any())
  {
    //... what goes here?
  }
}

我想保留所有信息(尤其是消息和堆栈跟踪)。

【问题讨论】:

标签: c# exception


【解决方案1】:

您正在寻找AggregateException class

【讨论】:

  • 啊,我需要 .net 4,但我知道我可以自己制作。谢谢。
【解决方案2】:

只需将您的列表捆绑到一个超级例外中:

class MultiException : Exception
{
    public List<Exception> ExceptionsList { get; set; }
}

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2015-04-29
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多