一、将执行try的return或者抛出异常情况
(1)try,catch语句块中均有return,finally语句块中无return,try语句块中抛出异常与catch要捕捉的异常类型不一致:将抛出try语句块中的异常。
(2)try,catch语句块中均有return,finally语句块中无return,三个语句块中都没有抛出异常:将执行try语句块的return。
举个例子,剩下的自己测试 // public class Test { public static int n = 1; public static void main(String[] args) throws ParseException { int result; result = temp(); System.out.println(result); } private static int temp() { try{ return n = n+1; }catch(Exception e){ return n = n+2; }finally { System.out.println("不管你怎么样,我都是要执行"); } } }最后程序输出:
不管你怎么样,我都是要执行
2
二、将执行catch的return或者抛出异常情况
(1)try,catch语句块中均有return,finally语句块中无return,try语句块中抛出异常且catch捕捉到异常:将执行catch语句块的return。
(2)try,catch语句块中均有return,finally语句块中无return,try语句块中抛出异常且catch捕捉到异常,catch语句块又抛出了异常:将抛出catch语句块中的抛出的异常。
三、将执行finally的return或者抛出异常情况
(注:try或catch中return后面的代码会执行,但最终返回的结果为finally中return的值,需要注意的是try或catch中return后面的代码会执行,只是存起来了,并没有返回)
(1)try,catch,finally语句块中均有return,try语句块中抛出异常且catch捕捉到异常:将执行finally语句块的return。
(2)try,catch,finally语句块中均有return,三个语句块中都没有抛出异常:将执行finally语句块中的return。
(3)try,catch,finally语句块中均有return,try语句块中抛出异常且catch捕捉到异常,catch语句块又抛出了异常:将执行finally语句块中的return。
(4)try,catch,finally语句块中均有return,try语句块中抛出异常与catch要捕捉的异常类型不一致:将执行finally语句块中的return。
(5)try,catch,finally语句块中均有return,try和catch语句块没有抛出异常,finally语句块中抛出异常:将抛出finally语句块中的异常。