欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-04 20:03:22
本文研究的主要是java中finally和return的关系,具体介绍和实例如下所示。 finally 和 return 关系的总结 1.try块中有system.ex...

本文研究的主要是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的关系实例解析

总结

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