try catch语句return返回值的原理是啥
public class Test {
public static void main(String[] args) {
Test t = new Test();
int result = t.xx();
System.out.println("运行完毕,返回的值为:"+result);
}
public int xx() {
int div = 10;
int result = 100;
try {
while(div>1) {
div--;
result = result+100/div;
}
System.out.println("程序正常运行,输出结果:"+result);
return result = 10;
}catch(Exception e) {
e.printStackTrace();
System.out.println("循环抛出异常");
}
finally {
System.out.println("fianlly执行了");
}
return result = 20;
}
}
代码如上,评论里有人说try catch的语句只要执行了return,那么语句块结束,但是视频里老师说try catch语句块结束之后不是会继续往下运行吗,那么我这里try语句中return返回了10,然后语句块结束,执行return result = 20; 最后main函数里接收到的值不应该是20吗?但是运行之后main函数里的值是10,这是为什么,到底谁说的对。还有:
public int xx() {
int div = 10;
int result = 100;
try {
while(div>0) {
div--;
result = result+100/div;
}
System.out.println("程序正常运行,输出结果:"+result);
return result = 10;
}catch(Exception e) {
e.printStackTrace();
System.out.println("循环抛出异常");
return result = 1;
}
finally {
System.out.println("fianlly执行了");
}
return result = 20;
}
写成这样return result = 20; 就会报错,为什么啊,之前在try里写了return,不是可以正常运行吗?怎么在catch里面写就报错呢,求解!!!!!!!!!!!