C# - 如果发生错误,则继续执行 foreach() 循环

发布于 2024-10-03 13:50:03 字数 216 浏览 1 评论 0原文

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

I currently have a listview and a folder full of XML documents. I am using a foreach() loop to go through all the XML files and load data into the listview accordingly. My question is, how do I carry on with the foreach() loop if there is an error within in (example: if one of the XML files is not completely valid, contains errors, etc) and still add the data to the listview? I'm not asking how to parse the XML or how to load it into the listview, that much I know how to do, just not how to proceed with the loop if an error occurs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦纸 2024-10-10 13:50:03

你想要:

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

Do you want:

foreach(var xml in xmls)
{
   try
   {
     //import xml to listview
   }
   catch (SomeException e)
   {
     //deal with the exception here
   }
}
但可醉心 2024-10-10 13:50:03

将循环的内部内容包装在 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)...
    }
}

Wrap the inner contents of the loop in a try ... catch block.

e.g.

foreach (var foo in iterableThing) {
    try {
        DoStuff(foo);
    }
    catch (AppropriateException) {
        // Handle the exception (or ignore it)...
    }
    catch (SomeOtherException) {
        // Handle the exception (or ignore it)...
    }
}
揽月 2024-10-10 13:50:03

你不会这样做吗

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

wouldnt you do

foreach( loop )
{
   try {
   }
   catch (Exception ex)
   {
      // all errors caught here, but the loop would continue
   }
}
奢望 2024-10-10 13:50:03

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

You can do the file processing in a try catch block and handle the error condition. You can handle the errors gracefully in catch and continue with loading of data.

痴者 2024-10-10 13:50:03

我认为你应该这样做:

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

I think you should do this:

foreach(var doc in docs)
{
    //Make a function to evaluate the doc
    if(isValid(doc))
    {
        //Logging or something
        continue;
    }
    //Add data to listview
}
白况 2024-10-10 13:50:03

如果您的处理代码引发异常,请使用 try/catch 块。如果您使用 if 块检查某个方法的结果,请使用 continue

If your processing code throws exceptions, then use a try/catch block. If you're checking the results of some method with an if block, then use continue.

樱娆 2024-10-10 13:50:03

如果您需要更频繁地使用它或者您只是想拥有更优雅的代码,您可以使用 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
}

If you need to use this more often or if you just want to have more elegant code, you can use lambda expressions and delegates to create a new abstraction for this purpose:

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

Then you can write just:

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

However, using exceptions in situations where an error is expected is not the best programming style. If you can write a method, say IsValid that returns true if the document is valid, then you could write:

foreach(var xml in xmls.Where(x => x.IsValid)) { 
  // Xml processing
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文