今天写程序遇到一个问题,就是需要在while循环 抛异常的时候 把 数据 return 出去。
发现 try 块 放在while 循环 外面 是正确的。
private static void test5() { int count=1; while (count<100) { count++; try { } catch (Exception e) { }finally{ System.out.println("final:"+count); } } }
结果
final:53
final:54
final:55
final:56
final:57
final:58
final:59
final:60
final:61
final:62
final:63
final:64
final:65
final:66
final:67
final:68
final:69
final:70
final:71
final:72
final:73
final:74
final:75
final:76
final:77
final:78
final:79
final:80
final:81
final:82
final:83
final:84
final:85
final:86
final:87
final:88
final:89
final:90
final:91
final:92
final:93
final:94
final:95
final:96
final:97
final:98
final:99
final:100
private static void test5() { int count=1; try { while (count<100) { count++; } } catch (Exception e) { }finally{ System.out.println("final:"+count);}