How can I break from a try/catch block without throwing an exception in Java

后端 未结 6 1581
感动是毒
感动是毒 2021-02-01 12:29

I need a way to break from the middle of try/catch block without throwing an exception. Something that is similar to the break and continue in for loops. Is this possible?

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 12:44

    This is the code I usually do:

     try
     {
        ...........
        throw null;//this line just works like a 'break'
        ...........   
      }
      catch (NullReferenceException)
      { 
      }
      catch (System.Exception ex)
      {
          .........
      }
    

提交回复
热议问题