C#使用Continue在try catch中捕获

Dig*_*yne 13 c# asp.net

现在,我对continue语句有一个重大问题.FetchUnseenMessages可能会也可能不会返回错误,具体取决于它是否能够连接到指定的电子邮件帐户.如果FetchUnseenMessages失败,我希望continue语句返回到foreach语句中的下一个项目(尝试下一个电子邮件帐户).我得到了一些意想不到的结果.我不相信continue语句会转到foreach语句中的下一个项目,但会回到try语句的开头并再次尝试它.我整天都被困在这里,而且我很困难.请帮忙.谢谢,克里斯.

foreach (string l in lUserName)
{ 
    try
    {
        newMessages = FetchUnseenMessages(sUsername);
    }
    catch
    {
        continue;
    }

    //Other code
}
Run Code Online (Sandbox Code Playgroud)

Adi*_*dil 26

您可以使用boolcatch块中设置的变量标志,并在catch之后执行continue语句if flag是否指示catch块的执行.

foreach (string l in lUserName)
{ 
   bool isError = false;  //flag would remain flase if no exception occurs 
   try
   {
       newMessages = FetchUnseenMessages();
   }
   catch
   {
       isError = true;
   }
   if(isError) continue;   //flag would be true if exception occurs
   //Other code 
}
Run Code Online (Sandbox Code Playgroud)

如果continue语句退出一个或多个具有关联的finally块的try块,则控制最初被转移到最里面的try语句的finally块.当控制到达finally块的结束点时,控制权转移到下一个封闭的try语句的finally块.重复此过程,直到执行了所有介入的try语句的finally块,msdn.

编辑通过给定的详细信息,continue的行为应该是正常的,不应该有任何问题.你可能在循环中有一些其他问题,比如闭包变量,你可以在这里阅读更多有关变量闭包的内容.

我做了一个测试来验证给定的场景,看起来很正常.

for (int i = 0; i < 3; i++)
{
    try
    {
    Console.WriteLine("Outer loop start");
    foreach (int l in new int[] {1,2,3})
    {       
        Console.WriteLine("Inner loop start");
        try
        {
            Console.WriteLine(l);
             throw new Exception("e");
        }
        catch
        {
            Console.WriteLine("In inner catch about to continue");
            continue;
        }           
        Console.WriteLine("Inner loop ends after catch");

    }
    Console.WriteLine("Outer loop end");
    }
    catch
    {
        Console.WriteLine("In outer catch");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出使用catch in catch

Outer loop start
Inner loop start
1
In inner catch about to continue
Inner loop start
2
In inner catch about to continue
Inner loop start
3
In inner catch about to continue
Outer loop end
Outer loop start
Inner loop start
1
In inner catch about to continue
Inner loop start
2
In inner catch about to continue
Inner loop start
3
In inner catch about to continue
Outer loop end
Outer loop start
Inner loop start
1
In inner catch about to continue
Inner loop start
2
In inner catch about to continue
Inner loop start
3
In inner catch about to continue
Outer loop end
Run Code Online (Sandbox Code Playgroud)

循环可变外壳

List<Func<int>> actions = new List<Func<int>>();
int variable = 0;
while (variable < 3)
{
    actions.Add(() => variable * variable);
    ++ variable;
}

foreach (var act in actions)
{
    Console.WriteLine(act.Invoke());
}
Run Code Online (Sandbox Code Playgroud)

循环外壳变量的输出

9
9
9
Run Code Online (Sandbox Code Playgroud)

循环变量封装的解决方案,制作循环变量的副本并将其传递给操作.

while (variable < 3)
{
    int copy = variable;
    actions.Add(() => copy * copy );
    ++ variable;
}
Run Code Online (Sandbox Code Playgroud)

产量

0
1
4
Run Code Online (Sandbox Code Playgroud)

  • 你应该提到`foreach`循环的闭包语义(不是像`for`或`while`这样的其他循环)从C#4变为C#5.所以如果你在一些匿名函数中捕获`foreach`声明的变量(通常是一个lambda表达式,带有`=>`箭头),结果取决于你的C#版本. (3认同)