亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2018年02月01日 14:01:00   作者:司馬懿字仲達  
這篇文章主要介紹了Java中finally和return的關(guān)系實例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

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

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

1.try塊中有System.exit(0)這樣的語句,由于它是終止Java虛擬機JVM的,連JVM都停止了,所有都結(jié)束了,當然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虛擬機JVM的,連JVM都停止了,所有都結(jié)束了,當然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é)果:

總結(jié)

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

相關(guān)文章

  • 可視化定時任務(wù)quartz集成解析全過程

    可視化定時任務(wù)quartz集成解析全過程

    在開發(fā)中有很多定時任務(wù)都不是寫死的而是可以人為配置并且寫到數(shù)據(jù)庫中的,下面這篇文章主要給大家介紹了關(guān)于可視化定時任務(wù)quartz集成解析的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Java把Map轉(zhuǎn)為對象的實現(xiàn)代碼

    Java把Map轉(zhuǎn)為對象的實現(xiàn)代碼

    在項目開發(fā)中,經(jīng)常碰到map轉(zhuǎn)實體對象或者對象轉(zhuǎn)map的場景,工作中,很多時候我們可能比較喜歡使用第三方j(luò)ar包的API對他們進行轉(zhuǎn)化,但這里,我想通過反射的方式對他們做轉(zhuǎn)化,感興趣的同學(xué)跟著小編來看看吧
    2023-08-08
  • 詳解Java String中intern方法的原理與使用

    詳解Java String中intern方法的原理與使用

    這篇文章主要為大家介紹了Java String中intern方法的原理以及使用。文中通過圖片和示例代碼進行了詳細展示,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • Java中的分布式事務(wù)Seata詳解

    Java中的分布式事務(wù)Seata詳解

    這篇文章主要介紹了Java中的分布式事務(wù)Seata詳解,Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡單易用的分布式事務(wù)服務(wù),Seata 將為用戶提供了 AT、TCC、SAGA 和 XA 事務(wù)模式,為用戶打造一站式的分布式解決方案,需要的朋友可以參考下
    2023-08-08
  • 當面試官問我ArrayList和LinkedList哪個更占空間時,我是這么答的(面試官必問)

    當面試官問我ArrayList和LinkedList哪個更占空間時,我是這么答的(面試官必問)

    今天介紹一下Java的兩個集合類,ArrayList和LinkedList,這兩個集合的知識點幾乎可以說面試必問的。感興趣的朋友跟隨小編一起看看吧
    2020-08-08
  • idea64.exe.vmoptions文件如何設(shè)置調(diào)整VM配置文件

    idea64.exe.vmoptions文件如何設(shè)置調(diào)整VM配置文件

    這篇文章主要介紹了idea64.exe.vmoptions文件如何設(shè)置調(diào)整VM配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • java發(fā)送http get請求的兩種方式

    java發(fā)送http get請求的兩種方式

    這篇文章主要為大家詳細介紹了java發(fā)送http get請求的兩種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Netty分布式高性能工具類recycler的使用及創(chuàng)建

    Netty分布式高性能工具類recycler的使用及創(chuàng)建

    這篇文章主要為大家介紹了Netty分布式高性能工具類recycler的使用和創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-03-03
  • java 文件流的處理方式 文件打包成zip

    java 文件流的處理方式 文件打包成zip

    這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 詳解Java中List的正確的刪除方法

    詳解Java中List的正確的刪除方法

    這篇文章主要為大家詳細介紹了Java中List的正確的刪除方法,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定幫助,需要的可以參考一下
    2022-05-05

最新評論