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

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

    In this sample in catch block i change the value of counter and it will break while block:

    class TestBreak {
        public static void main(String[] a) {
            int counter = 0;
    
            while(counter<5) {
                try {
                    counter++;
                    int x = counter/0;
                }
                catch(Exception e) {
                    counter = 1000;    
                }
            }
        }
    }k
    

提交回复
热议问题