亲宝软件园·资讯

展开

Java中finally和return的关系 Java中finally和return的关系实例解析

司马懿字仲达 人气:0
想了解Java中finally和return的关系实例解析的相关内容吗,司马懿字仲达在本文为您仔细讲解Java中finally和return的关系的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,finally,return,java,finally中return,java,finally,return,关系,下面大家一起来学习吧。

本文研究的主要是Java中finally和return的关系,具体介绍和实例如下所示。

finally 和 return 关系的总结

1.try块中有System.exit(0)这样的语句,由于它是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。
2.其它情况下,finally语句都必然会被执行。因此可以在这里执行一些资源的释放操作。

(1)finally中的return会覆盖try/catch中的renturn。
(2)在finally中写return语句会有警告,因为它会阻止函数抛出异常,而改为正常返回。

package com.demo.test;
public class FinallyAndReturn {
	private static void finallyAndTryReturn() {
		try {
			System.out.println("finallyAndTryReturn -> try");
			return;
		}
		catch (Exception e) {
			System.out.println("finallyAndTryReturn -> catch");
		}
		finally {
			System.out.println("finallyAndTryReturn -> finally");
		}
	}
	private static void finallyAndCatchReturn() {
		try {
			System.out.println("finallyAndCatchReturn -> try");
			throw new Exception();
		}
		catch (Exception e) {
			System.out.println("finallyAndCatchReturn -> catch");
			return;
		}
		finally {
			System.out.println("finallyAndCatchReturn -> finally");
		}
	}
	// finally语句是在try的return语句执行之后,return返回之前执行。
	private static String tryReturn() {
		String str = "initialized";
		try {
			System.out.println("tryReturn -> try");
			str = "try";
			return str;
		}
		catch (Exception e) {
			System.out.println("tryReturn -> catch");
			str = "catch";
		}
		finally {
			System.out.println("tryReturn -> finally");
			str = "finally";
		}
		return null;
	}
	private static String tryReturnAndFinallyReturn() {
		String str = "initialized";
		try {
			System.out.println("tryReturnAndFinallyReturn -> try");
			str = "try";
			return str;
		}
		catch (Exception e) {
			System.out.println("tryReturnAndFinallyReturn -> catch");
			str = "catch";
		}
		finally {
			System.out.println("tryReturnAndFinallyReturn -> finally");
			/*
       * Warning: finally block does not complete normally
       * 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
       */
			str = "finally";
			return str;
		}
	}
	private static String tryThrowAndFinallyReturn() throws Exception {
		String str = "initialized";
		try {
			System.out.println("tryThrowAndFinallyReturn -> try");
			str = "try";
			throw new Exception();
		}
		catch (Exception e) {
			System.out.println("tryThrowAndFinallyReturn -> catch");
			str = "catch";
			throw new Exception();
		}
		finally {
			System.out.println("tryThrowAndFinallyReturn -> finally");
			/*
       * Warning: finally block does not complete normally
       * 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
       */
			str = "finally";
			return str;
		}
	}
	private static void finallyAndRuntimeException() {
		try {
			System.out.println("finallyAndRuntimeException -> try");
			throw new RuntimeException();
		}
		catch (Exception e) {
			System.out.println("finallyAndRuntimeException -> catch");
		}
		finally {
			System.out.println("finallyAndRuntimeException -> finally");
		}
	}
	private static void finallyAndExit() {
		try {
			System.out.println("finallyAndExit -> try");
			// System.exit(0);是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。
			System.exit(0);
		}
		catch (Exception e) {
			System.out.println("finallyAndExit -> catch");
		}
		finally {
			System.out.println("finallyAndExit -> finally");
		}
	}
	public static void main(String[] args) {
		finallyAndTryReturn();
		System.out.println();
		finallyAndCatchReturn();
		System.out.println();
		System.out.println("tryReturn return -> " + tryReturn());
		System.out.println();
		System.out.println("tryReturnAndFinallyReturn return -> " + tryReturnAndFinallyReturn());
		System.out.println();
		try {
			System.out.println("tryThrowAndFinallyReturn return -> " + tryThrowAndFinallyReturn());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
		finallyAndRuntimeException();
		System.out.println();
		finallyAndExit();
	}
}

演示结果:

总结

以上就是本文关于Java中finally和return的关系实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论