try-catch-finally-return的执行顺序
总结如下几条情况
-
·try-catch-finally都有return语句时,没有异常时,返回值是finally中的return返回的·。
- 执行try块,执行到return语句时,先执行return的语句
- 但是不返回到main方法,执行finally块,遇到finally块中的return语句
- 并将值返回到main方法,这里就不会再回去返回try块中计算得到的值
- 所以最终的值是finally的返回
- 举例如下
public static void main(String[] args) throws ParseException {int i = NoException();System.out.println("最终值: " + i); }public static int NoException() {int i = 10;try {System.out.println("i in try block is:" + i);return --i;} catch (Exception e) {--i;System.out.println("i in catch - form try block is:" + i);return --i;} finally {System.out.println("i in finally - from try or catch block is:" + i);return --i;} } /* i in try block is:10 i in finally - from try or catch block is:9 最终值: 8*/
-
try-catch都有return语句时,没有异常时,返回值是try中的return返回的
- try中执行完return的语句后,不返回,执行finally块
- finally块执行结束后,返回到try块中
- 最后,返回在try块中最后的值
- 举例如下
public static void main(String[] args) throws ParseException {int i = NoException();System.out.println("最终值: " + i); }public static int NoException() {int i=10;try{System.out.println("i in try block is:"+i);return --i;}catch(Exception e){--i;System.out.println("i in catch - form try block is:"+i);return --i;}finally{System.out.println("i in finally - from try or catch block is:"+i);--i;System.out.println("i in finally block is:"+i);//return --i;} } /* i in try block is:10 i in finally - from try or catch block is:9 i in finally block is:8 最终值: 9 */
-
try块中抛出异常,try、catch和finally中都有return语句,返回值是finally中的return。
- 抛出异常后,执行catch块,在catch块的return的执行完后
- 并不直接返回而是执行finally,因finally中有return语句,所以,执行finally里面的语句
- 最终返回finally的return
- 举例如下
public static void main(String[] args) throws ParseException {int i = NoException();System.out.println("最终值: " + i); }public static int NoException() {int i = 10;try {System.out.println("i in try block is:" + i);i = i / 0;return --i;} catch (Exception e) {System.out.println("i in catch - form try block is:" + i);--i;System.out.println("i in catch block is:" + i);return --i;} finally {System.out.println("i in finally - from try or catch block is--" + i);--i;System.out.println("i in finally block is--" + i);return --i;} } /* i in try block is:10 i in catch - form try block is:10 i in catch block is:9 i in finally - from try or catch block is--8 i in finally block is--7 最终值: 6*/
-
try块中抛出异常,try和catch中都有return语句,返回的catch中return值。
- 抛出异常后,执行catch块
- 执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值。
- 举例如下
public static void main(String[] args) throws ParseException {int i = NoException();System.out.println("最终值: " + i); }public static int NoException() {int i = 10;try {System.out.println("i in try block is:" + i);i = i / 0;return --i;} catch (Exception e) {System.out.println("i in catch - form try block is:" + i);return --i;} finally {System.out.println("i in finally - from try or catch block is:" + i);--i;System.out.println("i in finally block is:" + i);//return i;} } /* i in try block is:10 i in catch - form try block is:10 i in finally - from try or catch block is:9 i in finally block is:8 最终值: 9 */
-
try、catch中都出现异常,在finally中有返回,返回finally中return值
- try块中出现异常到catch,catch中出现异常到finally
- finally中执行到return语句返回,不检查异常。
- 举例如下
public static void main(String[] args) throws ParseException {int i = NoException();System.out.println("最终值: " + i); }public static int NoException() {int i = 10;try {System.out.println("i in try block is:" + i);i = i / 0;return --i;} catch (Exception e) {System.out.println("i in catch - form try block is:" + i);int j = i / 0;return --i;} finally {System.out.println("i in finally - from try or catch block is:" + i);--i;System.out.println("i in finally block is:" + i);return --i;} } /* i in try block is:10 i in catch - form try block is:10 i in finally - from try or catch block is:10 i in finally block is:9 最终值: 8 */
try-with-resource语法糖
JDK7之后,Java多了个新的语法:try-with-resources语句,该语句确保在语句执行完毕后,每个资源都被自动关闭 。
可以理解为是一个声明一个或多个资源的 try语句(用分号隔开),一个资源作为一个对象,并且这个资源必须要在执行完关闭的,
其实之前的手动关闭,在这个语法糖里面并没有被取消,而是因为编译器,编译器自动帮我们生成了finally块,并且在里面调用了资源的close方法,所以例子中的close方法会在运行的时候被执行。
原理
传统写法
public void readFile() throws FileNotFoundException {FileReader fr = null;BufferedReader br = null;try{fr = new FileReader("d:/test.txt");br = new BufferedReader(fr);String s = "";while((s = br.readLine()) != null){System.out.println(s);}} catch (IOException e) {e.printStackTrace();}finally {try {br.close();fr.close();} catch (IOException e) {e.printStackTrace();}}
}
使用当前语法
public void readFile() throws FileNotFoundException {try(FileReader fr = new FileReader("d:/test.txt");BufferedReader br = new BufferedReader(fr)){String s = "";while((s = br.readLine()) != null){System.out.println(s);}} catch (IOException e) {e.printStackTrace();}
}