finally块中的代码什么时候被执行

问题描述:try{ }里有一个return语句,那么紧跟在这个try后的finally{ }中的代码是否会被执行?如果会的话什么时候被执行,在return之前还是之后?

第一段代码:
public class Test01 {
	public static int testFinally() {
		try {
			return 1;
		} catch (Exception e) {
			return 0;
		}finally {
			System.out.println("Finally块中的代码");
		}
	}
	public static void main(String[] args) {
		int result = testFinally();
		System.out.println(result);
	}
}
//Finally块中的代码
//1

由于程序执行return就意味着结束对当前函数的调用并跳出这个函数体,因此任何语句要执行都只能在return前执行(除非碰到exit函数),因此finally块里的代码也是在return前执行的


第二段代码
public class Test01 {
	public static int testFinally() {
		try {
			return 1;
		} catch (Exception e) {
			return 0;
		}finally {
			System.out.println("Finally块中的代码");
			return 3;
		}
	}
	public static void main(String[] args) {
		int result = testFinally();
		System.out.println(result);
	}
}
//Finally块中的代码
//3

当finally块中有return语句时,将会覆盖函数中其他return语句。


第三段代码
public class Test01 {
	public static int testFinally() {
		int result = 1;
		try {
			result = 2;
			return result;
		} catch (Exception e) {
			return 0;
		}finally {
			result = 3;
			System.out.println("Finally块中的代码");
		}
	}
	public static void main(String[] args) {
		int resultVal = testFinally();
		System.out.println(resultVal);
	}
}
//Finally块中的代码
//2

由于在一个方法内部定义的变量都存储在栈中,当这个函数结束后,其对应的栈就会被回收,此时在其方法体中定义的变量将不存在了,因此return在返回时不是直接返回变量的值,而是复制一份,然后返回。即,程序在调用return前,先把result值2复制一份存储在一个指定的位置,然后再去执行finally块中的代码,此时修改result的值将不会影响程序的返回结果。因此,对于第三段代码所测的基本数据类型的数据在finally块中改变return的值对返回值没有任何影响


如果对于引用数据类型呢?

第四段代码
public class Test01 {
	public static StringBuffer testFinally() {
		StringBuffer s = new StringBuffer("Hello");
		try {
			return s;
		} catch (Exception e) {
			return null;
		}finally {
			s.append(" World");
			System.out.println("Finally块中的代码");
		}
	}
	public static void main(String[] args) {
		StringBuffer resultRef = testFinally();
		System.out.println(resultRef);
	}
}
//Finally块中的代码
//Hello World

虽然程序在调用return前,先把resultRef的值复制一份s存储到一个指定位置,然后再去执行finally块中的代码,但是原s依然可以引用到该字符串,所以可以改变该字符串,所以得出结论:对于引用数据类型来说,在finally块中改变return的值对返回值有影响。


引申:出现在Java程序中的finally块是不是一定会被执行?

首先答案时否定的。

  • 当程序在进入try语句块之前就出现异常,会直接结束。
public class Test01 {
	public static void testFinally() {
		int i = 5/0;
		try {
			System.out.println("try block");
		} catch (Exception e) {
			System.out.println("catch block");
		}finally {
			System.out.println("Finally block");
		}
	}
	public static void main(String[] args) {
		testFinally();
	}
}
/**
 * Exception in thread "main" java.lang.ArithmeticException: / by zero
 *	at Test.Test01.testFinally(Test01.java:5)
 *  at Test.Test01.main(Test01.java:15)
 */
  • 当程序在try块中强制退出时不会去执行finally块中的代码。
public class Test01 {
	public static void testFinally() {
		try {
			System.out.println("try block");
			System.exit(0);
		} catch (Exception e) {
			System.out.println("catch block");
		}finally {
			System.out.println("Finally block");
		}
	}
	public static void main(String[] args) {
		testFinally();
	}
}
//try block

猜你喜欢

转载自blog.csdn.net/weixin_40995778/article/details/83546262