我想让try块中出现异常时,在catch中继续调用contextLoads()执行。为什么for中的变量z在第二次调用contextLoads()时开始与全局变量 i 不一致? 怎样才能使他们保持一致?
class KafkaTestApplicationTests {
int i = 1;
int sum = 1;
@Test
void contextLoads() {
for(int z = i;z < 6;z++) {
System.out.println("z = " + z + " , " + "i = " + i);
i = i + 1;
operation();
}
}
void operation() {
try {
int c = 2/sum;
sum = sum -1;
System.out.println(c);
} catch (Exception e) {
contextLoads();
}
}
}
z = 1 , i = 1
2
z = 2 , i = 2
z = 3 , i = 3
z = 4 , i = 4
z = 5 , i = 5
z = 5 , i = 6
z = 4 , i = 7
z = 5 , i = 8
z = 3 , i = 9
z = 4 , i = 10
z = 5 , i = 11
不一致的同时变量 z 的值还没有规律。