天天看點

try catch finally執行順序和return值

概述:

1、finally中的語句塊總會執行,最後執行

2、如果try語句塊中出現能被catch捕捉的異常、那麼異常語句之後的代碼将不會執行。

3、如果try或catch或finally中有return語句執行了,那麼finally語句塊後的語句将不會執行(此條具體結論看最後總結,此處不太嚴謹)

測試代碼如下:

package com.test;


public class TryCatchFinally {

	public static void main(String[] args) {
		TryCatchFinally tr=new TryCatchFinally();
		int result=tr.test();
		System.out.println("the final result is "+result);
	}
	

	


	public int test(){
		int a=10;
		int b=0;
		try{
			System.out.println("this is try start");
			int c=a/b;
			c=c++;
			System.out.println("this is try end");
			return 1;
		}catch(Exception e){
			System.out.println("this is catch");
			//return 2;
		}finally {
			System.out.println("this is finally");
//			return 3;
		}
		System.out.println("88");
//		return 0;
}
}
           

情況1:try{}catch{}finally{}retrun

<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">						</span>this is try start</span></div><span style="white-space:pre">			</span>this is catch
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">						</span>88</span></div><span style="white-space:pre">			</span>this is finally
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">						</span>the final result is 0</span></div>
           

按順序執行

情況2:try{return}catch{}finally{}

報錯

情況3:try{return}catch{return}finally{}

<span style="white-space:pre">			</span>this is try start
<span style="white-space:pre">			</span>this is catch
<span style="white-space:pre">			</span>this is finally
<span style="white-space:pre">			</span>the final result is 2
           

傳回值為catch中的return,并且try中異常語句後的代碼不執行

情況4:try{return}catch{return}finally{return}

<span style="white-space:pre">			</span>this is try start
<span style="white-space:pre">			</span>this is catch
<span style="white-space:pre">			</span>this is finally
<span style="white-space:pre">			</span>the final result is 3
           

傳回值為finally中的return

情況5:try{}catch{return}finally{return}

結果同上

情況6:try{}catch{return}finally{}

報錯

情況7:try{}catch{return}finally{}return

<span style="white-space:pre">			</span>this is try start
<span style="white-space:pre">			</span>this is catch
<span style="white-space:pre">			</span>this is finally
<span style="white-space:pre">			</span>the final result is 2
           

傳回值為catch中的return,finally後的語句不執行

情況8:try{}catch{}finally{return}

<span style="white-space:pre">			</span>this is try start
<span style="white-space:pre">			</span>this is catch
<span style="white-space:pre">			</span>this is finally
<span style="white-space:pre">			</span>the final result is 3
           

傳回值為fianlly中的return

總結:

1、finally中的return較catch中return“等級”高,即若catch與finally中均出現return時,最終傳回的是finally中的return

2、若隻有catch中有return語句,finally語句後可添加代碼,而且必須有return語句,因為編譯器并不知道程式會不會出現異常。若程式無異常,fianlly後程式正常執行。若出現異常,傳回的值為catch中return的值,而且finally後的代碼不會執行。

3、try或finally中有return語句,finally後面任何代碼都不能添加。編譯器會提示代碼不可達,會報錯。

(個人總結,僅供參考。如若有錯,歡迎指正。)