问题描述
在此code样,有没有办法继续从catch块外循环?
而
{
//外环 而
{
//内环
尝试
{
扔;
}
抓住
{
//我怎么继续从这里外循环?
继续;
}
}
}
更新:这个问题是的感谢伟大的问题!
继续和突破只不过是一个愉快的语法,更多的转到。显然,给他们可爱的名字,并限制其用途特定的控制结构,他们不再画了所有的goto都是坏的所有的时间的人群的愤怒。
如果你想要做的是继续到外,你的可能的只需在外环的顶部定义了一个标签,然后选择转到这个标签。如果你觉得这样做并不妨碍code的COM prehensibility,那么这可能是最有利的解决方案。
不过,我想借此为契机,考虑你的控制流是否会从一些重构中受益。每当我有有条件的破发和继续,在嵌套循环,我认为重构。
考虑:
successfulCandidate = NULL;
的foreach(考生VAR候选人)
{
的foreach(在标准VAR标准)
{
如果(!candidate.Meets(标准))//编辑。
{// TODO:在持续的检查标准,没有任何意义。
// TODO:不知何故,继续外循环来检查一个候选
}
}
successfulCandidate =候选人;
打破;
}
如果(successfulCandidate!= NULL)//做一些事情
两种重构技术:
首先,提取内环的方法:
的foreach(考生VAR候选人)
{
如果(MeetsCriteria(候选,标准))
{
successfulCandidate =候选人;
打破;
}
}
其次,可以的所有的环被淘汰?如果你是循环的,因为你想寻找的东西,然后重构它到一个查询。
VAR结果,从候选=候选人
其中,criteria.All(标准=> candidate.Meets(标准))
选择候选人;
变种successfulCandidate = results.FirstOrDefault();
如果(successfulCandidate!= NULL)
{
做一些与候选人
}
如果没有环路那么就没有必要打破或继续!
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;
}
}
}
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:
successfulCandidate = null;
foreach(var candidate in candidates)
{
foreach(var criterion in criteria)
{
if (!candidate.Meets(criterion)) // Edited.
{ // TODO: no point in continuing checking criteria.
// TODO: Somehow "continue" outer loop to check next candidate
}
}
successfulCandidate = candidate;
break;
}
if (successfulCandidate != null) // do something
Two refactoring techniques:
First, extract the inner loop to a method:
foreach(var candidate in candidates)
{
if (MeetsCriteria(candidate, criteria))
{
successfulCandidate = candidate;
break;
}
}
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.
var results = from candidate in candidates
where criteria.All(criterion=>candidate.Meets(criterion))
select candidate;
var successfulCandidate = results.FirstOrDefault();
if (successfulCandidate != null)
{
do something with the candidate
}
If there are no loops then there is no need to break or continue!
这篇关于继续在嵌套while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!