java 中try catch finally 与return位置的关系

来源:互联网 发布:c语言判断数字的位数 编辑:程序博客网 时间:2024/06/06 12:56

通过实际代码说明:

1.代码在finally块之后有rerun

public class Test {
public static void main(String[] args) {
int x = getin(6);
System.out.println("返回的值:"+x);
}

public static int getin(int x){
int y=0;
try {

y =x/2;
System.out.println("try中的y:"+y);

} catch (Exception e) {
e.printStackTrace();
System.out.println("catch中的y:"+y);
}finally{
System.out.println("finally中的y:"+y);

}
System.out.println("测试是否finally代码块后的代码--执行");
return y;
}
}

----------------------------执行结果----------------------------------------

try中的y:3
finally中的y:3
测试是否finally代码块后的代码--执行
返回的值:3

2,此时如果在finally中改变y的值那么返回值会是finally块中改变的值

public class Test {
public static void main(String[] args) {
int x = getin(6);
System.out.println("返回的值:"+x);
}

public static int getin(int x){
int y=0;
try {

y =x/2;
System.out.println("try中的y:"+y);

} catch (Exception e) {
e.printStackTrace();
System.out.println("catch中的y:"+y);

}finally{
y=10;
System.out.println("finally中的y:"+y);

}
System.out.println("测试是否finally代码块后的代码--执行");
return y;
}
}

-------------------------执行结果-----------------------------

try中的y:3
finally中的y:10
测试是否finally代码块后的代码--执行
返回的值:10

3.在try中有return,并在finally改变y的值,返回的值依然为try块中y的值,此时如果未发生异常,finally代码块后的代码是执行不到的,因为在执行完finally后会到try块中返回

public class Test {
public static void main(String[] args) {
int x = getin(6);
System.out.println("返回的值:"+x);
}

public static int getin(int x){
int y=0;
try {

y =x/2;
System.out.println("try中的y:"+y);

return y;
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch中的y:"+y);

}finally{
y=10;
System.out.println("finally中的y:"+y);

}
System.out.println("测试是否finally代码块后的代码--执行");
return y;
}
}

--------------------执行结果-----------虽然finally改变了值,但是返回的依然是try块中的值------------------

try中的y:3
finally中的y:10
返回的值:3

4.如果在finally中改变值的同时,加上return,那么返回的值就是改变后的值。

public class Test {
public static void main(String[] args) {
int x = getin(6);
System.out.println("返回的值:"+x);
}

public static int getin(int x){
int y=0;
try {

y =x/2;
System.out.println("try中的y:"+y);

return y;
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch中的y:"+y);

}finally{
y=10;
System.out.println("finally中的y:"+y);
return y;
}

}
}

------------------执行结果---------------------

try中的y:3
finally中的y:10
返回的值:10

5.当发送异常时,并在catch中返回,在finally改变y值,那么结论也是一样的返回的值为catch块中的值。

public class Test {
public static void main(String[] args) {
int x = getin(6);
System.out.println("返回的值:"+x);
}

public static int getin(int x){
int y=0;
try {

y =x/2;
System.out.println("try中的y:"+y);
int c = 2/0;
return y;
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch中的y:"+y);
return y;
}finally{
y=10;
System.out.println("finally中的y:"+y);

}

}
}

--------------执行结果---------------

try中的y:3
java.lang.ArithmeticException: / by zero
at com.test.Test.getin(Test.java:16)
at com.test.Test.main(Test.java:6)
catch中的y:3
finally中的y:10
返回的值:3

6,try catch finally中都存在return 并且在finally中修改y的值,那么返回的是finally中的值

public class Test {
public static void main(String[] args) {
int x = getin(6);
System.out.println("返回的值:"+x);
}

public static int getin(int x){
int y=0;
try {

y =x/2;
System.out.println("try中的y:"+y);

return y;
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch中的y:"+y);
return y;
}finally{
y=10;
System.out.println("finally中的y:"+y);
return y;
}

}
}

----------------------执行结果--------------------------

try中的y:3
finally中的y:10
返回的值:10


6,由此可见,

1)return语句不建议放在finally中,

2)如果在执行finally块之前有return语句(并且finally中无return),那么即使finally中改变了返回的值,

返回的值也是为改变前的值,

3)如果finally中存在return,那么try catch块中的return就得不到执行,返回的值为finally修改后的值。

4)如果try,catch,finally中都无return,只在最后有return,并且finally修改了要返回的值,

那么返回的将是修改后的值。

5)return语句可行的位置:

a.return语句只在函数最后出现一次。

b.return语句仅在try和catch里面都出现。

c.return语句仅在try和函数的最后都出现。

d.return语句仅在catch和函数的最后都出现。

注意,除此之外的其他做法都是不可行的,编译器会报错。


0 0
原创粉丝点击