Continue in nested while loops

后端 未结 10 1574
名媛妹妹
名媛妹妹 2020-12-07 14:26

In this code sample, is there any way to continue on the outer loop from the catch block?

while
{
   // outer loop

   while
   {
       // inner loop
               


        
10条回答
  •  孤城傲影
    2020-12-07 15:13

    I think the best way to accomplish this would be to use the break statement. Break ends the current loop and continues execution from where it ends. In this case, it would end the inner loop and jump back into the outer while loop. This is what your code would look like:

    while
    {
       // outer loop
    
       while
       {
           // inner loop
           try
           {
               throw;
           }
           catch 
           {
               // break jumps to outer loop, ends inner loop immediately.
               break; //THIS IS THE BREAK
           }
       }
    }
    

    I believe that is what you were looking to be accomplished, correct? Thanks!

提交回复
热议问题