作者forris (乔巴)
看板java
标题[问题] try-catch-finally 叙述
时间Tue Apr 8 01:15:51 2008
有一个程式是这样的:
public class test{
public static void main(String[] args){
int x = 0;
while (x<10){
try{
if (x==3) continue;
if (x==4) break;
}
finally{
System.out.println("x="+x);
if (x==4) continue;
x = x + 1;
}
}
System.out.println("final x="+x);
}
}
执行结果会是:
x=0
x=1
x=2
x=3
x=4
x=4
x=4
......
这是一个无穷回圈
问为什麽 x=3 and x=4 竟会执行?且 x=4 是个无穷回圈?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.116.193.15
1F:→ forris:continue and break 没有发生作用? 04/08 01:16
2F:推 slalala:FINALLY一定会执行 无论有无break 04/08 02:31
3F:推 AI3767:这很明显啊@@ 仔细看看当x为4时, 每行是怎麽执行的... 04/08 10:28
4F:推 heal92013:在下试了一下,如果把final中的if那一行//後就有答案了 04/08 10:59
5F:推 fuu0115:break-> if (x==4) cont';->x还是4 就变无穷回圈了 04/08 16:27
6F:推 Lecwar:x=4的时候在finally里就不会再增加..当然就无穷loop罗 04/09 16:14