c#在尝试捕获的捕获过程中继续使用。

时间:2022-06-02 22:19:39

Now, I'm having a major problem with the continue statement. FetchUnseenMessages may or may not return an error depending on whether or not it's able to connect to a specified Email account. I want the continue statement to go back up to the next item in the foreach statement (trying the next email account) should FetchUnseenMessages fail. I'm getting some unexpected results. I don't believe the continue statement is going to the next item in the foreach statement but going back to the beginning of the try statement and trying it again. I've been stuck on this all day and I'm pretty stuck. Please help. Thanks, Chris.

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

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

    //Other code
}

1 个解决方案

#1


23  

You can use a bool variable flag set in catch block and execute continue statement after catch if flag indicates the execution of catch block.

您可以在catch块中使用一个bool变量标志,并在catch if标记指示执行catch块后执行continue语句。

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 
}

If the continue statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed, msdn.

如果continue语句退出一个或多个带有关联的finally块的try块,那么控制将首先转移到最内部try语句的finally块。当控制到达最后一个块的端点时,控制被转移到下一个封闭的try语句的finally块。这个过程重复进行,直到执行所有中间的try语句的最终块,msdn。

Edit By the given details the behaviour of the continue should be normal not you should not have any problems. You might have some other problem like closure variable in a loop, you can read more about variable closure here.

编辑给定的细节,继续的行为应该是正常的,而不是你不应该有任何问题。您可能还有其他问题,比如循环中的闭包变量,您可以在这里阅读更多关于变量闭包的内容。

I have made a test to verify the given scenario and it appears to be normal.

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

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");
    }
}

The output using continue in catch

在catch中使用continue的输出

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

Loop variable enclosure

循环变量圈地

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());
}

Output of loop enclosure variable

循环外壳变量的输出

9
9
9

Solution of loop variable enclosure, make a copy of loop variable and pass it to action.

解决循环变量外壳,复制循环变量并传递给动作。

while (variable < 3)
{
    int copy = variable;
    actions.Add(() => copy * copy );
    ++ variable;
}

Output

输出

0
1
4

#1


23  

You can use a bool variable flag set in catch block and execute continue statement after catch if flag indicates the execution of catch block.

您可以在catch块中使用一个bool变量标志,并在catch if标记指示执行catch块后执行continue语句。

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 
}

If the continue statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed, msdn.

如果continue语句退出一个或多个带有关联的finally块的try块,那么控制将首先转移到最内部try语句的finally块。当控制到达最后一个块的端点时,控制被转移到下一个封闭的try语句的finally块。这个过程重复进行,直到执行所有中间的try语句的最终块,msdn。

Edit By the given details the behaviour of the continue should be normal not you should not have any problems. You might have some other problem like closure variable in a loop, you can read more about variable closure here.

编辑给定的细节,继续的行为应该是正常的,而不是你不应该有任何问题。您可能还有其他问题,比如循环中的闭包变量,您可以在这里阅读更多关于变量闭包的内容。

I have made a test to verify the given scenario and it appears to be normal.

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

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");
    }
}

The output using continue in catch

在catch中使用continue的输出

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

Loop variable enclosure

循环变量圈地

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());
}

Output of loop enclosure variable

循环外壳变量的输出

9
9
9

Solution of loop variable enclosure, make a copy of loop variable and pass it to action.

解决循环变量外壳,复制循环变量并传递给动作。

while (variable < 3)
{
    int copy = variable;
    actions.Add(() => copy * copy );
    ++ variable;
}

Output

输出

0
1
4