java异常:(实例分析)try-catch-finally 中哪个部分可以省略?如果 catch 中 return 了,finally 还会执行吗?return的执行顺序?

1、try-catch-finally 中哪个部分可以省略?

以下三种情况都是可以的:
try-catch
try-finally
try-catch-finally
可以省略catch或者finally。catch和finally不可以同时省略。

2、如果 catch 中 return 了,finally 还会执行吗?

会。

(1)finally的作用就是,无论出现什么状况,finally里的代码一定会被执行。

(2)如果在catch中return了,也会在return之前,先执行finally代码块。

(3)而且如果finally代码块中含有return语句,会覆盖其他地方的return。

(4)对于基本数据类型的数据,在finally块中改变return的值对返回值没有影响,而对引用数据类型的数据会有影响。

注:
finally也不是一定会被执行。

3、什么情形下,finally代码块不会执行?

(1)没有进入try代码块;

(2)System.exit()强制退出程序;

(3)守护线程被终止;

详细分析见博客:finally代码块不被执行的情况总结

4、return的执行顺序到底是怎样的?

我们先通过几个代码实例来看一下~

(1)、基本数据类型

(1)finally中的return覆盖try中的return,返回result=1。

	public static int testTryCatch1() {
		int result = 0;
		try {
			result = 1;
			return result+1;
		} catch (Exception e) {
			return result;
		} finally {
			return result;
		}
	}//返回1

(2)在try找那个return之前,将基本数据类型的变量1存储在栈中,finally中对result改变,不会影响try中的返回值。

	public static int testTryCatch2() {
		int result = 0;
		try {
			result = 1;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;
		}
	}
	//返回1

(3)finally中的return覆盖catch中的return。

	public static int testTryCatch5() {
		int result = 0;
		try {
			result = 1/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;
			return result;
		}
	}
	//返回2

(4)finally中的return覆盖try中的return。

	public static int testTryCatch1() {
		int result = 0;
		try {
			result = 1;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;
			return result;
		}
	}
	//返回2

(5)在执行catch中的return之前,把返回值0存储在栈中,所以finally中对于result的操作不影响返回值,所以返回0。

	public static int testTryCatch6() {
		int result = 0;
		try {
			result = 1/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;	
		}
	}
	//返回0

(2)、引用数据类型

以下四个方法,返回结果分别是AB、AB、AB、AB。

由此可见,对于引用数据类型,return之前,是将引用变量result存储在栈中,所以即使finally中对引用变量进行操作,也会影响返回值。

/**
	 * 引用数据类型try_finally中return的执行
	 * @return
	 */
	public static StringBuilder testTryCatch3() {
		StringBuilder result = new StringBuilder("A");
		try {
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		    return result;
		}
	}
	
	public static StringBuilder testTryCatch4() {
		StringBuilder result = new StringBuilder("A");
		try {
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		}
	}
	/**
	 * 引用数据类型catch_finally中return的执行
	 * @return
	 */
	
	public static StringBuilder testTryCatch7() {
		StringBuilder result = new StringBuilder("A");
		try {
			int i=5/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		    return result;
		}
	}
	
	public static StringBuilder testTryCatch8() {
		StringBuilder result = new StringBuilder("A");
		try {
			int i=5/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		}
	}
	

(3)、总结

(1)首先,finally中return肯定会覆盖try或者catch中的返回值。

(2)return之前必须会执行finally代码块,对于finally中没有return的语句来说:
如果返回值是基本数据类型,finally块中对返回值的改变不会影响返回值。因为在return之前已经将返回值的内容存储在栈中了。
如果返回值是引用数据类型,finally块中对返回值的改变会影响返回值。因为在return之前已经将引用对象的地址存储在栈中,finally块中对于引用对象值的改变会影响到返回值。

  • 25
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值