在循环中如果有异常被catch住,但是同时不想中断循环可以这样:
for(int i=-1;i<3;i++){
try {
System.out.println("result ="+i);
if(1/i!= 0){
System.out.println("result ="+i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
console结果为:
result =-1
result =-1
result =0
java.lang.ArithmeticException: / by zero
at MyTask.run(ttt.java:23)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
result =1
result =1
result =2
这样就好了
时间: 2024-10-16 12:59:20