日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

try catch finally 中包含return的几种情况,及返回结果

發(fā)布時間:2025/3/12 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 try catch finally 中包含return的几种情况,及返回结果 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

第一種情況:在try和catch中有return,finally中沒有return,且finally中沒有對try或catch中要 return數(shù)據(jù)進(jìn)行操作的代碼,這種情況也是最好理解的。

public class Test {public static int num=1;public static void main(String[] args) throws ParseException {int result;result = num();System.out.println(result);}private static int num() {try{int b=4/0;return num = num+2;}catch(Exception e){return num = num+3;}finally {System.out.println("不管你怎么樣,我都是要執(zhí)行"); } } }

輸出內(nèi)容為:
不管你怎么樣,我都是要執(zhí)行
4

原因,int b=4/0;發(fā)生了異常,直接進(jìn)入catch的代碼塊中執(zhí)行了return num = num+3;此時把返回的結(jié)果4。如果沒有異常的話會執(zhí)行try中的return,catch中的代碼不會執(zhí)行,但是無論怎樣,finally中的代碼都會執(zhí)行。

第二種情況:在try和catch中有return,finally中沒有return,但finally中有對try或catch中要 return數(shù)據(jù)進(jìn)行操作的代碼

要返回的數(shù)據(jù)是基本數(shù)據(jù)類型還是引用數(shù)據(jù)類型,對結(jié)果也有不同的影響

①返回的數(shù)據(jù)為基本數(shù)據(jù)類型,則finally中對要返回數(shù)據(jù)操作無影響

public class Test {public static int num=1;public static void main(String[] args){int result;result = num();System.out.println(result);//結(jié)果不受finally影響,輸出4System.out.println(num);//5}private static int num() {try{int b=4/0;return num = num+2;}catch(Exception e){return num = num+3;}finally {++num;} } }

result的值為4的原因是,當(dāng)執(zhí)行到catch中的 return num = num+3;時,已經(jīng)把要返回的num的值存到了其他局部變量中,在執(zhí)行完finally中的++num;后,是從其他局部變量中獲取的返回值,而不是直接返回num的值

②返回的數(shù)據(jù)為引用數(shù)據(jù)類型,finally中如果改變了返回對象的屬性則影響結(jié)果,如果改變的是對象的引用則和基本數(shù)據(jù)類型一樣不改變結(jié)果

public class Test {public static void main(String[] args) {People bride;bride = marry();System.out.println(bride.getState());//結(jié)果受finally影響,輸出dead}private static People marry() {People people=new People();people.setState("happy");;try{int b=4/0;return people;}catch(Exception e){return people;}finally {people.setState("dead");} } }

bride.getState()的結(jié)果為dead的原因是,當(dāng)執(zhí)行到catch中的return people;時,把要返回people的內(nèi)存地址存儲起來,但是finally中對該內(nèi)存地址對象的屬性進(jìn)行了更改,bride = marry();
獲取的內(nèi)存地址對應(yīng)的對象是更改屬性后的people,所以屬性值改變了。

第三種情況:在try和catch中有return,finally中也有return

try或catch中return后面的代碼會執(zhí)行,但最終返回的結(jié)果為finally中return的值,需要注意的是try或catch中return后面的代碼會執(zhí)行,只是存起來了,并沒有返回,讓finally捷足先登先返回了

public class Test {public static int num=1;public static void main(String[] args) throws ParseException {int result;result = num();System.out.println(result);//輸出結(jié)果為1003System.out.println(num);//輸出結(jié)果為1001}private static int num() {try{int b=4/0;return num = num+1000;}catch(Exception e){return num = num+1000;}finally {return num+2;} } }


第四種情況:在try中有return,在catch中新拋出異常,finally中有return

如果catch塊中捕獲了異常, 并且在catch塊中將該異常throw給上級調(diào)用者進(jìn)行處理, 但finally中有return, 那么catch塊中的throw就失效了, 上級方法調(diào)用者是捕獲不到異常

public class Test {public static void main(String[] args) throws Exception {String result="";try {result = num();} catch (Exception e) {System.out.println("青天大老爺在此");}System.out.println(result);}public static String num() throws Exception {try{int b=4/0;return "總是異常,反正我又不會執(zhí)行";}catch(Exception e){throw new Exception();}finally {return "用金錢蒙蔽你的雙眼";} } }

以上代碼輸出:

用金錢蒙蔽你的雙眼

如果把finally里的return注釋掉就會輸出:
青天大老爺在此

結(jié)束語:try catch finally的情感糾紛到此結(jié)束,finally的用處是很大的。

它是為異常處理事件提供的一個清理機(jī)制,一般是用來關(guān)閉文件或釋放其他系統(tǒng)資源。

finally只有一種情況不會執(zhí)行。當(dāng)執(zhí)行到System.exit(0);finally就不會執(zhí)行。

總結(jié)

以上是生活随笔為你收集整理的try catch finally 中包含return的几种情况,及返回结果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。