try-catch-finally中finally的执行时机

昨天同事问了一个问题:

public class TestException {
	public TestException() {
	}

	boolean testEx() throws Exception {
		boolean ret = true;
		try {
			ret = testEx1();
		} catch (Exception e) {
			System.out.println("testEx, catch exception");
			ret = false;
			throw e;
		} finally {
			System.out.println("testEx, finally; return value=" + ret);
			return ret;
		}
	}

	boolean testEx1() throws Exception {
		boolean ret = true;
		try {
			ret = testEx2();
			if (!ret) {
				return false;
			}
			System.out.println("testEx1, at the end of try");
			return ret;
		} catch (Exception e) {
			System.out.println("testEx1, catch exception");
			ret = false;
			throw new Exception();
		} finally {
			System.out.println("testEx1, finally; return value=" + ret);
			return ret;
		}
	}

	boolean testEx2() throws Exception {
		boolean ret = true;
		try {
			int b = 12;
			int c;
			for (int i = 2; i >= -2; i--) {
				c = b / i;
				System.out.println("i=" + i);
			}
			return true;
		} catch (Exception e) {
			System.out.println("testEx2, catch exception");
			ret = false;
			throw new Exception();
		} finally {
			System.out.println("testEx2, finally; return value=" + ret);
			return ret;
		}
	}

	public static void main(String[] args) {
		TestException testException1 = new TestException();
		try {
			System.out.println(testException1.testEx());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


上边的代码执行的时候,testEx2的方法的catch块的代码会执行,testEx1和testEx方法的catch代码就不执行了。

之前一直没在finally里return过,没出过啥问题,虽然看过几篇关于try-catch-finally相关的文章,但也一直没有在意。调试了一下代码,发现确实是没有执行testEx1和testEx的代码块。

回忆了下之前看到的几篇文章的内容,想到可能是finally的执行时机的问题。finally的执行时机是方法返回之前。分析了一下代码,是这样子滴:

在testEx2中,try中的代码会报错,于是执行catch中的内容,catch的最后一句是抛出一个错误,我们知道在执行这句代码的时候,方法就结束了。但是这个try-catch还有个finally,在执行throw new  Exception之前,先执行finally里的代码,而finally里有个return,在finally里直接返回了,throw new Exception也就没执行,testEx2方法也就没有抛出Exception,在testEx1中也就catch不到了,testEx中更catch不到Exception了。

为了验证上边的解释,testEx2的finally的return直接返回true。

public class TestException {
	public TestException() {
	}

	boolean testEx() throws Exception {
		boolean ret = true;
		try {
			ret = testEx1();
		} catch (Exception e) {
			System.out.println("testEx, catch exception");
			ret = false;
			throw e;
		} finally {
			System.out.println("testEx, finally; return value=" + ret);
			return ret;
		}
	}

	boolean testEx1() throws Exception {
		boolean ret = true;
		try {
			ret = testEx2();
			if (!ret) {
				return false;
			}
			System.out.println("testEx1, at the end of try");
			return ret;
		} catch (Exception e) {
			System.out.println("testEx1, catch exception");
			ret = false;
			throw new Exception();
		} finally {
			System.out.println("testEx1, finally; return value=" + ret);
			return ret;
		}
	}

	boolean testEx2() throws Exception {
		boolean ret = true;
		try {
			int b = 12;
			int c;
			for (int i = 2; i >= -2; i--) {
				c = b / i;
				System.out.println("i=" + i);
			}
			return true;
		} catch (Exception e) {
			System.out.println("testEx2, catch exception");
			ret = false;
			throw new Exception();
		} finally {
			System.out.println("testEx2, finally; return value=" + ret);
			return true;
		}
	}

	public static void main(String[] args) {
		TestException testException1 = new TestException();
		try {
			System.out.println(testException1.testEx());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


调试一下代码可以看到,testEx2会返回true,在testEx1中会执行testEx2返回true的逻辑,testEx也是执行testEx1返回true的逻辑。

最佳实践是在finally中执行一些资源释放,比如关个流,关个连接什么的,不在finally里写return。去掉testEx2,testEx1和testEx方法中finally里的return语句。变成如下的代码:

public class TestException {
	public TestException() {
	}

	boolean testEx() throws Exception {
		boolean ret = true;
		try {
			ret = testEx1();
		} catch (Exception e) {
			System.out.println("testEx, catch exception");
			ret = false;
			throw e;
		} finally {
			System.out.println("testEx, finally; return value=" + ret);
		}
		return ret;
	}

	boolean testEx1() throws Exception {
		boolean ret = true;
		try {
			ret = testEx2();
			if (!ret) {
				return false;
			}
			System.out.println("testEx1, at the end of try");
			return ret;
		} catch (Exception e) {
			System.out.println("testEx1, catch exception");
			ret = false;
			throw new Exception();
		} finally {
			System.out.println("testEx1, finally; return value=" + ret);
		}
	}

	boolean testEx2() throws Exception {
		boolean ret = true;
		try {
			int b = 12;
			int c;
			for (int i = 2; i >= -2; i--) {
				c = b / i;
				System.out.println("i=" + i);
			}
			return true;
		} catch (Exception e) {
			System.out.println("testEx2, catch exception");
			ret = false;
			throw new Exception();
		} finally {
			System.out.println("testEx2, finally; return value=" + ret);
		}
	}

	public static void main(String[] args) {
		TestException testException1 = new TestException();
		try {
			System.out.println(testException1.testEx());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


这时候逻辑就非常好理解了,从testEx2抛出的Exception会一直传递到testEx方法,最终传递到main方法。

finally的执行时机到这就有个大体的概念了,就是方法返回之前(正常返回【return】,异常返回【throw】),执行。下面把这个时间点再精确一点。

运行下面的代码看看:

public class TestFinally {
	public static void main(String[] args) {
		System.out.println(increment());
	}

	public static Mode increment() {
		Mode t = new Mode();
		try {
			t.field = 1;
			if (t.field == 1) {
				throw new NullPointerException();
			}
			return t;
		} catch (Exception e) {
			t.field = 7;
			return increment2(t);
		} finally {
			t.field = 3;
		}

	}

	private static Mode increment2(Mode mode) {
		mode.field++;
		return mode;
	}
}

class Mode {
	public int field = 4;

	@Override
	public String toString() {
		return String.format("Mode {field=%s}", field);
	}
}


上面的代码输入结果是t.a=3。可以看出,increment方法是中的catch中的return,是先执行了increment2方法,然后再执行finally里的代码,最后执行的return。

总结一句,finally是在方法执行返回之前结束。具体的时间粒度可以看上边喽。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈振阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值