继续嵌套的 while 循环
在此代码示例中,是否有任何方法可以从 catch 块继续执行外循环?
while
{
// outer loop
while
{
// inner loop
try
{
throw;
}
catch
{
// how do I continue on the outer loop from here?
continue;
}
}
}
In this code sample, is there any way to continue on the outer loop from the catch block?
while
{
// outer loop
while
{
// inner loop
try
{
throw;
}
catch
{
// how do I continue on the outer loop from here?
continue;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
你只想打破内在,而内在却会延续外在。
You just want to break from the inner which would continue the outer.
如果您想使用 try catch,只需将它们向外移动(尽管我建议重构)。
只有当它是一个错误并且需要记录时我才会这样做,我也会推送
If you wish to use try catch, simply move them outwards (although I would recommend refactoring).
I would only do this if it IS an error and needs logging, I would also push
我认为实现此目的的最佳方法是使用 break 语句。 Break 结束当前循环并从结束处继续执行。 在这种情况下,它将结束内部循环并跳回外部 while 循环。 这就是您的代码的样子:
我相信这就是您想要完成的,对吗?
谢谢!
I think the best way to accomplish this would be to use the break statement. Break ends the current loop and continues execution from where it ends. In this case, it would end the inner loop and jump back into the outer while loop. This is what your code would look like:
I believe that is what you were looking to be accomplished, correct?
Thanks!
使用自己的异常类型,例如 MyException。 然后:
这将适用于继续和打破多层嵌套 while 语句。
抱歉,格式错误;)
Use an own exception type, e.g., MyException. Then:
This will work for continuing and breaking out of several levels of nested while statements.
Sorry for bad formatting ;)
更新:这个问题是我关于这个主题的文章的灵感。< /a> 感谢您提出这个好问题!
“继续”和“中断”只不过是“转到”的令人愉快的语法。 显然,通过给它们起可爱的名字并将它们的使用限制在特定的控制结构上,它们不再引起“所有的 goto 一直都是坏的”人群的愤怒。
如果您想要做的是继续到外部,您可以简单地在外部循环的顶部定义一个标签,然后“转到”该标签。 如果您认为这样做不会妨碍代码的可理解性,那么这可能是最方便的解决方案。
但是,我会以此为契机考虑您的控制流是否会从某些重构中受益。 每当我在嵌套循环中有条件“中断”和“继续”时,我就会考虑重构。
考虑:
两种重构技术:
第一,将内部循环提取到方法中:
第二,可以消除所有循环吗? 如果您因为尝试搜索某些内容而进行循环,请将其重构为查询。
如果没有循环,则无需中断或继续!
UPDATE: This question was inspiration for my article on this subject. Thanks for the great question!
"continue" and "break" are nothing more than a pleasant syntax for a "goto". Apparently by giving them cute names and restricting their usages to particular control structures, they no longer draw the ire of the "all gotos are all bad all the time" crowd.
If what you want to do is a continue-to-outer, you could simply define a label at the top of the outer loop and then "goto" that label. If you felt that doing so did not impede the comprehensibility of the code, then that might be the most expedient solution.
However, I would take this as an opportunity to consider whether your control flow would benefit from some refactoring. Whenever I have conditional "break" and "continue" in nested loops, I consider refactoring.
Consider:
Two refactoring techniques:
First, extract the inner loop to a method:
Second, can all the loops be eliminated? If you are looping because you are trying to search for something, then refactor it into a query.
If there are no loops then there is no need to break or continue!
问题解决了。 (什么?为什么你们都用那种肮脏的眼神看我?)
Problem solved. (what?? Why are you all giving me that dirty look?)
你可以利用休息时间; 陈述。
continue 用于跳回到当前循环的顶部。
如果您需要突破更多级别,则必须添加某种“if”或使用可怕/不推荐的“goto”。
You can use a break; statement.
Continue is used to jump back to the top of the current loop.
If you need to break out more levels than that you will either have to add some kind of 'if' or use the dreaded/not recommended 'goto'.
将 try/catch 结构与内部 while 循环交换:
Swap the try/catch structure with the inner while loop:
编号
我建议将内部循环提取到一个单独的方法中。
No.
I suggest, extracting the inner loop into a separate method.
在内循环中使用
break
。Use
break
in the inner loop.