1.try。。。catch。。。finally。。。执行顺序?
无论是否有异常,finally里面的语句都会执行的;
如果是在try{...}代码中间部分出现异常,try{...}异常后的代码不执行,但try。。。catch。。。finally下方的代码照常执行;
2.catch里面有return语句,finally会执行吗?如果过会,return是在finally前执行还是后执行?
---会,finally优先与catch执行
/**
* @author Herbert
* @create 2019-06-02 08:56
* catch里面有return语句,finally会执行吗?如果过会,return是在finally前执行还是后执行?
* ---会,finally优先与catch执行
*/
public class Test {
public static void main(String[] args) {
int r= getDiv(10,0);
System.out.println("r="+r);
}
public static int getDiv(int a , int b){
int i;
try {
i= a/b;
System.out.println("异常后代码执行了。。。");
return i;
}catch (Exception e){
System.out.println("分母不能为0");
i=-1;
return i; //如果r=-1,代表catch里的return先执行,然后是finally
}finally {
System.out.println("finally被执行了。。。");
i=-2; //如果R=-2,代表finally先执行,然后是catch里的return
return i;
}
}
}
运行结果:
分母不能为0
finally被执行了。。。
r=-2
Process finished with exit code 0