Continue in nested while loops

后端 未结 10 1552
名媛妹妹
名媛妹妹 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:29

    Use an own exception type, e.g., MyException. Then:

    while
    {
       try {
       // outer loop
       while
       {
           // inner loop
           try
           {
               throw;
           }
           catch 
           {
               // how do I continue on the outer loop from here?
               throw MyException;
           }
       }
       } catch(MyException)
       { ; }
    }
    

    This will work for continuing and breaking out of several levels of nested while statements. Sorry for bad formatting ;)

提交回复
热议问题