Continue in nested while loops

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

    using System;
    
    namespace Examples
    {
    
        public class Continue : Exception { }
        public class Break : Exception { }
    
        public class NestedLoop
        {
            static public void ContinueOnParentLoopLevel()
            {
                while(true)
                try {
                   // outer loop
    
                   while(true)
                   {
                       // inner loop
    
                       try
                       {
                           throw new Exception("Bali mu mamata");
                       }
                       catch (Exception)
                       {
                           // how do I continue on the outer loop from here?
    
                           throw new Continue();
                       }
                   }
                } catch (Continue) {
                       continue;
                }
            } 
        }
    
    }
    
    }
    

提交回复
热议问题