當當當,興致勃勃的第二篇博客,散花~
下面是正題(敲黑板)
第一種情況:在try和catch中有return,finally中沒有return,且finally中沒有對try或catch中要 return數據進行操作的代碼,這種情況也是最好理解的。
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("不管你怎么樣,我都是要執行"); } } }
輸出內容為:
不管你怎么樣,我都是要執行
4
原因,int b=4/0;發生了異常,直接進入catch的代碼塊中執行了return num = num+3;此時把返回的結果4。如果沒有異常的話會執行try中的return,catch中的代碼不會執行,但是無論怎樣,finally中的代碼都會執行。
第二種情況:在try和catch中有return,finally中沒有return,但finally中有對try或catch中要 return數據進行操作的代碼
要返回的數據是基本數據類型還是引用數據類型,對結果也有不同的影響
①返回的數據為基本數據類型,則finally中對要返回數據操作無影響
public class Test { public static int num=1; public static void main(String[] args){ int result; result = num(); System.out.println(result);//結果不受finally影響,輸出4 System.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的原因是,當執行到catch中的 return num = num+3;時,已經把要返回的num的值存到了其他局部變量中,在執行完finally中的++num;后,是從其他局部變量中獲取的返回值,而不是直接返回num的值
②返回的數據為引用數據類型,finally中如果改變了返回對象的屬性則影響結果,如果改變的是對象的引用則和基本數據類型一樣不改變結果
public class Test { public static void main(String[] args) { People bride; bride = marry(); System.out.println(bride.getState());//結果受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()的結果為dead的原因是,當執行到catch中的return people;時,把要返回people的內存地址存儲起來,但是finally中對該內存地址對象的屬性進行了更改,bride = marry();
獲取的內存地址對應的對象是更改屬性后的people,所以屬性值改變了。
第三種情況:在try和catch中有return,finally中也有return
try或catch中return后面的代碼會執行,但最終返回的結果為finally中return的值,需要注意的是try或catch中return后面的代碼會執行,只是存起來了,並沒有返回,讓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);//輸出結果為1003 System.out.println(num);//輸出結果為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給上級調用者進行處理, 但finally中有return, 那么catch塊中的throw就失效了, 上級方法調用者是捕獲不到異常
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 "總是異常,反正我又不會執行"; }catch(Exception e){ throw new Exception(); }finally { return "用金錢蒙蔽你的雙眼"; } } }
以上代碼輸出:
用金錢蒙蔽你的雙眼
如果把finally里的return注釋掉就會輸出:
青天大老爺在此
結束語:try catch finally的情感糾紛到此結束,大家看完是不是覺得finally就是一個小婊咂呢?其實finally的用處是很大的。
它是為異常處理事件提供的一個清理機制,一般是用來關閉文件或釋放其他系統資源。
finally只有一種情況不會執行。當執行到System.exit(0);finally就不會執行。