如题。
我写了一个demo,代码如下:
public class Test {
public static int method1() {
int b =0;
try{
throw new Exception();
}catch (Exception e) {
System.out.println("in catch b="+b);
return b=-1;
}finally{
System.out.println("in finally:b="+b);
b = 1;
}
}
public static void main(String[] args) {
System.out.println("result b ="+method1());
}
}
结果:
in catch b=0
in finally:b=-1
result b =-1
足以证明,先执行catch中的return,然后再执行finally!
我写了一个demo,代码如下:
public class Test {
public static int method1() {
int b =0;
try{
throw new Exception();
}catch (Exception e) {
System.out.println("in catch b="+b);
return b=-1;
}finally{
System.out.println("in finally:b="+b);
b = 1;
}
}
public static void main(String[] args) {
System.out.println("result b ="+method1());
}
}
结果:
in catch b=0
in finally:b=-1
result b =-1
足以证明,先执行catch中的return,然后再执行finally!
本文通过一个简单的Java示例代码,演示了在Java中catch块中的return语句与finally块的执行顺序。实验结果表明,catch块中的return会被首先执行,然后才是finally块的内容。
962

被折叠的 条评论
为什么被折叠?



