溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中finally和return的關(guān)系實例解析

發(fā)布時間:2020-09-07 11:04:42 來源:腳本之家 閱讀:138 作者:司馬懿字仲達(dá) 欄目:編程語言

本文研究的主要是Java中finally和return的關(guān)系,具體介紹和實例如下所示。

finally 和 return 關(guān)系的總結(jié)

1.try塊中有System.exit(0)這樣的語句,由于它是終止Java虛擬機(jī)JVM的,連JVM都停止了,所有都結(jié)束了,當(dāng)然finally語句也不會被執(zhí)行到。
2.其它情況下,finally語句都必然會被執(zhí)行。因此可以在這里執(zhí)行一些資源的釋放操作。

(1)finally中的return會覆蓋try/catch中的renturn。
(2)在finally中寫return語句會有警告,因為它會阻止函數(shù)拋出異常,而改為正常返回。

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語句執(zhí)行之后,return返回之前執(zhí)行。
	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塊重新拋出了異常,則調(diào)用該方法的語句也不會獲得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塊重新拋出了異常,則調(diào)用該方法的語句也不會獲得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虛擬機(jī)JVM的,連JVM都停止了,所有都結(jié)束了,當(dāng)然finally語句也不會被執(zhí)行到。
			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();
	}
}

演示結(jié)果:

Java中finally和return的關(guān)系實例解析

總結(jié)

以上就是本文關(guān)于Java中finally和return的關(guān)系實例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI