【问题标题】:C# - Proceed with foreach() loop if an error occursC# - 如果发生错误,则继续执行 foreach() 循环
【发布时间】:2011-05-11 04:50:06
【问题描述】:

我目前有一个列表视图和一个装满 XML 文档的文件夹。我正在使用 foreach() 循环遍历所有 XML 文件并将数据相应地加载到列表视图中。我的问题是,如果其中有错误(例如:如果其中一个 XML 文件不完全有效,包含错误等),我该如何继续 foreach() 循环并且仍然将数据添加到列表视图?我不是在问如何解析 XML 或如何将其加载到列表视图中,我知道该怎么做,只是没有在发生错误时如何继续循环。

【问题讨论】:

    标签: c# xml error-handling foreach


    【解决方案1】:

    你想要吗:

    foreach(var xml in xmls)
    {
       try
       {
         //import xml to listview
       }
       catch (SomeException e)
       {
         //deal with the exception here
       }
    }
    

    【讨论】:

    • 比我快 1 秒 ;-)
    • 好吧,我觉得自己像个白痴。我在 try/catch 中有 foreach() 循环,但从未想过要反过来,现在我觉得自己智障了,哈哈。谢谢。
    • @Nate - 看看我的问题让自己感觉良好 :) stackoverflow.com/questions/1723741/…
    【解决方案2】:

    将循环的内部内容包装在 try ... catch 块中。

    例如

    foreach (var foo in iterableThing) {
        try {
            DoStuff(foo);
        }
        catch (AppropriateException) {
            // Handle the exception (or ignore it)...
        }
        catch (SomeOtherException) {
            // Handle the exception (or ignore it)...
        }
    }
    

    【讨论】:

      【解决方案3】:

      你会吗

      foreach( loop )
      {
         try {
         }
         catch (Exception ex)
         {
            // all errors caught here, but the loop would continue
         }
      }
      

      【讨论】:

        【解决方案4】:

        您可以在 try catch 块中进行文件处理并处理错误情况。您可以在 catch 中优雅地处理错误并继续加载数据。

        【讨论】:

          【解决方案5】:

          我认为你应该这样做:

          foreach(var doc in docs)
          {
              //Make a function to evaluate the doc
              if(isValid(doc))
              {
                  //Logging or something
                  continue;
              }
              //Add data to listview
          }
          

          【讨论】:

            【解决方案6】:

            如果您的处理代码抛出异常,则使用try/catch 块。如果您使用if 块检查某些方法的结果,请使用continue

            【讨论】:

              【解决方案7】:

              如果您需要更频繁地使用它,或者您只想拥有更优雅的代码,您可以使用 lambda 表达式和委托来为此创建新的抽象:

              static void SafeForEach<T>(this IEnumerable<T> source, Action<T> op) {
                foreach(var el in source) {
                  try { op(el); }
                  catch (Exception e) { }
                }
              }
              

              那么你可以写:

              xmls.SafeForEach(xml => {
                   // Xml processing
                });
              

              但是,在预期会出错的情况下使用异常并不是最好的编程风格。如果您可以编写一个方法,例如 IsValid,如果文档有效则返回 true,那么您可以编写:

              foreach(var xml in xmls.Where(x => x.IsValid)) { 
                // Xml processing
              }
              

              【讨论】:

                猜你喜欢
                • 2022-06-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-04-06
                相关资源
                最近更新 更多