package cn.java.se.exception;
/**
* 总结:
* 1.如果try中没有异常,那么最终的返回值当然是try返回值;如果try中有异常被catch捕获,那么最终的返回值为 catch返回值;
* try {
* 可能发送异常的代码;
* return try返回值;
* }catch {
* return catch返回值;
* }
*
* 2.在try 中遇到return 就转到 finally 中。所以,最终返回的值是 finally 中return 的值;
* try {
* return try返回值;
* }finally {
* 输出语句;
* return finally返回值;
* }
*
* 3.在catch 中遇到return 就转到 finally 中。 所以,最终的返回值是finally 中return 的值;
* try {
* 可能发送异常的代码;
* }catch {
* return catch返回值;
* } finally {
* return finally返回值;
* }
*
* 问题:
* 1.finally 中的代码一定会执行吗? 在JVM 启动的情况下,一定会执行;并且是在return 语句前执行;
*
* Created by zhaojd on 2017/4/20.
*/
public class TestFinally {
public static void main(String[] args) {
System.out.println(finallyIsOrExecute());
}
public static int finallyIsOrExecute() {
int[] arr = new int[2];
try {
int i = arr[2];
System.out.println(i);
return 10;
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
return 20;
}finally {
System.out.println("执行的前后顺序");
//return 30;
}
}
}
finally中的内容一定会执行吗?极其执行的前后顺序?
最新推荐文章于 2023-04-09 17:00:43 发布