//看一下下面的程序,你能正確的寫出不同的testEx2()方法時,程序的最終打印出來的數據嗎....先不要看下面的答案
public class ExceptionTest { public ExceptionTest() { } boolean testEx() throws Exception { boolean ret = true; try { ret = testEx1(); } catch (Exception e) { System.out.println("testEx, catch exception"); ret = false; throw e; } finally { System.out.println("testEx, finally; return value=" + ret); return ret; } } boolean testEx1(){ boolean ret = true; try { ret = testEx2(); if(!ret){return false;}System.out.println("testEx1, at the end of try"); return ret;} catch (Exception e) { System.out.println("testEx1, catch exception"); ret = false; throw e; } finally { System.out.println("testEx1, finally; return value=" + ret); return ret; } } //第一種:/* boolean testEx2() throws Exception{ boolean ret = true; try { int b = 12; int c; for (int i = 2; i >= -2; i--) { c = b / i;System.out.println("i=" + i); } return ret; } catch (Exception e) { System.out.println("testEx2, catch exception"); ret = false; throw e;} finally { System.out.println("testEx2, finally; return value=" + ret); return ret; } } *///第二種:boolean testEx2() throws Exception { boolean ret = true; int b = 12; int c; for (int i = 2; i >= -2; i--) { c = b / i; System.out.println("i=" + i); } System.out.printf("這句話打打出來了嗎??????");return true; }//第三種:/*boolean testEx2() throws Exception{ boolean ret = true; try { int b = 12; int c; for (int i = 2; i >= -2; i--) { c = b / i;System.out.println("i=" + i); } return ret; } catch (Exception e) { System.out.println("testEx2, catch exception"); ret = false; throw e;} finally { System.out.println("testEx2, finally; return value=" + ret); //return ret; //此處不寫return 語句} //System.out.println("fhsdfsdofi");//因為try中有一個直接return語句,所以這兩句不能被訪問//return ret;} *///第四種:/*boolean testEx2() throws Exception{ boolean ret = true; try { int b = 12; int c; for (int i = 2; i >= -2; i--) { c = b / i;System.out.println("i=" + i); } } catch (Exception e) { System.out.println("testEx2, catch exception"); //ret = false; throw e;} finally { System.out.println("testEx2, finally; return value=" + ret); //return ret; //此處不寫return 語句} System.out.println("這句話打印出來了!!!!");return ret;} *///第五種:/*boolean testEx2() throws Exception{ //提醒一下,第四種和第五種只有catch中有沒有 throw e 不一樣boolean ret = true; try { int b = 12; int c; for (int i = 2; i >= -2; i--) { c = b / i;System.out.println("i=" + i); } //return ret; } catch (Exception e) { System.out.println("testEx2, catch exception"); ret = false; //throw e;} finally { System.out.println("testEx2, finally; return value=" + ret); //return ret; //此處不寫return 語句} System.out.println("這句話打印出來了!!!!!!!");return ret;}*/public static void main(String[] args) { ExceptionTest testException1 = new ExceptionTest(); try { testException1.testEx(); } catch (Exception e) { e.printStackTrace(); } }
} class myException extends Exception{public void printException(){System.out.println("產生異常!");}
}/*異常看著容易,理解起來也很容易!但是它真的沒有你想像的那么簡單!
第一種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false第二種:
i=2
i=1
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false第三種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false第四種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=true
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false第五種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
這句話打印出來了!!!!!!!
testEx1, finally; return value=false
testEx, finally; return value=false總結一下:
一:throw new Exception()第一種情形:(由第二種情形驗證)int test() throws Exception{if(....)throw new Exception();....return x;}第二中情形:第五種情況可驗證int test() throws Exception{try{if(...)throw new Exception();}catch(ArithmeticException e){//在try中產生的異常沒有被捕獲時:....}finally{....}.....return x;}在執行到throw 時,第一種先將return返回個調用者(也就是throw和return之間的語句不會被執行),然后再調用程序中尋找處理異常的程序第二種還要將 finally中的語句執行完(即異常有沒有被立即處理都要執行finally),(throw和return之間的語句也不會被執行),然后執行return語句,程序轉向調用者中尋找異常處理程序。
二:再讓我們看一下finally中寫return的一些缺憾
1 finally塊中的return語句會覆蓋try塊、catch塊中的return語句
2 如果finally塊中包含了return語句,即使前面的catch塊重新拋出了異常,則調用該方法的語句也不會獲得catch塊重新拋出的異常,
而是會得到finally塊的返回值,并且不會捕獲異常,也就是如果在catch或者try中產生的異常如果在向外界拋出是不可能的。。。。第一種情況:testEx2()方法中會產生一個ArithmeticException的異常, Exception是它的父類,我們在該方法中捕獲了該異常并進行了處理
所以會輸出 testEx2()中的 catch 和 finally 中的輸出數據, 而在testEx1()方法中,調用了testEx2(),由于testEx2()中的異常已經被處理
并且由于finally中的return語句導致testEx2()中catch中的throw e 無法重新拋出,所以在testEx1()中不會被捕獲。再說一下第四,第五種情況:fianlly中都沒有return
在第四種情況中, 由于在catch中繼續拋出了該異常,該異常在testEx2()中沒有被處理,所以在執行finally之后的語句(除了return語句)不會被執行,
而第五種是沒有繼續拋出該異常,也就是textEx2()中產生的異常全部被處理了,所以finally之后的語句會被執行.....其他不多說。如果還是沒有搞懂的話,推薦看一下下面這個鏈接,寫的不錯....
http://blog.csdn.net/hguisu/article/details/6155636
*/