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.
foreach (string l in lUserName)
{
try
{
newMessages = FetchUnseenMessages(sUsername);
}
catch
{
continue;
}
//Other code
}
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".
It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With