gpt4 book ai didi

C# 在 try catch 的 catch 中使用 Continue

转载 作者:可可西里 更新时间:2023-11-01 08:04:05 24 4
gpt4 key购买 nike

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

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

//Other code
}

最佳答案

您可以在 catch block 中使用 bool 变量标志集,如果标志指示执行 catch block ,则在 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 的行为应该是正常的,您不应该有任何问题。您可能还有其他问题,例如 closure variable in a loop ,您可以阅读更多关于变量闭包的信息 here .

我已经做了一个测试来验证给定的场景,它似乎是正常的。

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

在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

循环变量封装

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

循环封装变量的输出

9
9
9

loop variable enclosures的解决方案,复制一个loop variable传给action。

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

输出

0
1
4

关于C# 在 try catch 的 catch 中使用 Continue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com