C# 如何在循环中处理异常并保持迭代?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/564229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How handle an exception in a loop and keep iterating?
提问by
I need a for loop which will complete all its Iterations even if there's any exception in any one of the iterations.
我需要一个 for 循环,即使在任何一次迭代中出现任何异常,它也能完成所有迭代。
采纳答案by teedyay
for (...)
{
try
{
// Do stuff
}
catch (Exception ex)
{
// Handle (or ignore) the exception
}
}
回答by Rosstified
Just put each iteration inside a try..catch
只需将每次迭代放在 try..catch 中
foreach(Person a in people)
{
try
{
WorkOnPerson(a);
}
catch
{
// do something if you want to.
}
}
回答by Sesh
There is no inbuilt feature in a loopto do that. That is no language feature built in C# that will automatically handle an exception and continue with a loop.
循环中没有内置功能可以做到这一点。这不是 C# 中内置的语言功能,它会自动处理异常并继续循环。
Also to the extent possible, please avoid putting try-catch blocks inside the loop. Even though it solves the problem you mention, consider that the compiler & run-time have to do that much additional job. If there were no exceptions occurring all that would go waste.
此外,请尽可能避免将 try-catch 块放入循环中。即使它解决了您提到的问题,请考虑编译器和运行时必须做那么多额外的工作。如果没有异常发生,所有这些都会浪费掉。
Instead let the exceptions be exceptions. That is something that occurs exceptionally: outside your designed input considerations. Of course, this is only a suggestion, if you really have to continue the loop, go with the two options suggested above.
相反,让例外成为例外。这是一种特殊情况:在您设计的输入考虑之外。当然,这只是一个建议,如果你真的要继续循环,就用上面建议的两个选项。
回答by David Bo?jak
Well the thing is ... Your solution will have to include a for loop and some sort of error/exception handling process, so you will probably have to embed a try catch statement in your for loop.
嗯,事情是......你的解决方案必须包括一个 for 循环和某种错误/异常处理过程,所以你可能必须在你的 for 循环中嵌入一个 try catch 语句。
If an exception is thrown, there is no way you will complete that one iteration as you would if the exception wasn't thrown. However by using an try catch you can make sure that your loop executes all those iterations that don't throw exceptions.
如果抛出异常,则无法像未抛出异常时那样完成该迭代。但是,通过使用 try catch,您可以确保您的循环执行所有不引发异常的迭代。
If you need help with embedding exception handling in a for loop, just use the example posted by teedyay!
如果您需要有关在 for 循环中嵌入异常处理的帮助,只需使用 teedyay 发布的示例!
回答by robi-y
Or, if this is a recurring pattern in your program, and you're taking the risk for this catch all exception style, wrap it as an extension to your collections. Applying it to the previous example:
或者,如果这是您程序中的重复模式,并且您要承担捕获所有异常样式的风险,请将其包装为您的集合的扩展。将其应用于前面的示例:
people.ForEachIgnorant(ofThrowingWorkOnPerson);
Or:
或者:
people.ForEachIgnorant(p => WorkOnPersonThatThrows(p));
Implementation:
执行:
public static void IgnorantForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
{
try
{
action(item);
}
catch { }
}
}
回答by Peanut
I think it's also worth noting that if you are using a generic List - you could use the following to iterate the collection:
我认为还值得注意的是,如果您使用的是通用列表 - 您可以使用以下内容来迭代集合:
ForEach(Action action)
ForEach(Action 动作)
http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx
http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx
EmployeesList.ForEach(ProcessEmployee);
void ProcessEmployee(Employee employeeItem)
{
try
{
...
}
catch { }
}
This has the benefit of making the code in your loop reusable.
这有利于使循环中的代码可重用。
回答by kmo
Do you know what the exception is and what will cause it? Can you test for it and prevent it being thrown, ie. CanCompleteStep or TryCompleteStep. if you cant complete just skip that step. You can then put exception handling out of the loop.
你知道异常是什么以及什么会导致它吗?你能测试它并防止它被抛出,即。CanCompleteStep 或 TryCompleteStep。如果您无法完成,请跳过该步骤。然后,您可以将异常处理置于循环之外。