public static void main(String[] args) {
// // TODO Auto-generated method stub
try {
System.out.println("how‘s the weather today?");
} catch (Exception e) {
System.out.println("i don‘t know");
}finally{
System.out.println("weather is fine");
}
Output:
how‘s the weather today?
weather is fine
这个大家都懂吧,我就不说什么了。
如果我加return的话,结果会怎么样?上测试代码:
public static void main(String[] args) {
// // TODO Auto-generated method stub
try {
System.out.println("how‘s the weather today?");
return;
} catch (Exception e) {
System.out.println("i don‘t know");
return;
}finally{
System.out.println("weather is fine");
}
结果还是:
how‘s the weather today?
weather is fine
代码走完,可能有些人得出结论:finally一定会执行。
不要着急,还没有完,再跑一段代码:
public static void main(String[] args) {
// // TODO Auto-generated method stub
try {
System.out.println("how‘s the weather today?");
System.exit(0);
} catch (Exception e) {
System.out.println("i don‘t know");
}finally{
System.out.println("weather is fine");
}
Output:
how‘s the weather today?
观看输出发现finally块并没有被执行,JVM都退出了,还怎么运行呢。所以以后有人问的话,可以这样说,在JVM正常运行的情况下,finally块一定会执行。
finally块中的代码一定会执行吗?,布布扣,bubuko.com
原文地址:http://my.oschina.net/u/617909/blog/292565