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

后端 未结 6 1415
感动是毒
感动是毒 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:47

    You can always do it with a break from a loop construct or a labeled break as specified in aioobies answer.

    public static void main(String[] args) {
        do {
            try {
                // code..
                if (condition)
                    break;
                // more code...
            } catch (Exception e) {
    
            }
        } while (false);
    }
    

提交回复
热议问题