try中有一個(gè)異常語句,后面只跟了一個(gè)finally, 返回上一級(jí)的時(shí)候?yàn)槭裁床蹲讲坏疆惓#?/h1>
package com.imooc;
import javax.xml.transform.TransformerException;
public class TestException { ?
? ? public TestException() { ?
? ? } ?
??
? ? 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() throws Exception { ?
? ? ? ? boolean ret = true; ?
? ? ? ? try { ?
? ? ? ? ? ? ret = testEx2(); ?
? ? ? ? ? ? System.out.println("ret=="+ret);
? ? ? ? ? ? //System.out.println("echang=="+e);
? ? ? ? ? ? if (!ret) { ?
? ? ? ? ? ? ? ? return false; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? System.out.println("testEx1, at the end of try"); ?
? ? ? ? ? ? return ret; ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? System.out.println("testEx1游戲場(chǎng), catch exception"); ?
? ? ? ? ? ? ret = false; ?
? ? ? ? ? ? throw e; ?
? ? ? ? } finally { ?
? ? ? ? //System.out.println("異常是");
? ? ? ? ? ? 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 true; ?
? ? ? ? } /*catch (NullPointerException e) { ?
? ? ? ? ? ? System.out.println("testEx2低級(jí)異常, catch exception"); ?
? ? ? ? ? ? ret = false; ?
? ? ? ? ? ? //System.out.println("e=="+e);
? ? ? ? ? ? throw e; ?
? ? ? ? ? ? //System.out.println("能不能執(zhí)行?");
? ? ? ? } */finally { ?
? ? ? ? ? ? System.out.println("testEx2低級(jí)最終要執(zhí)行, finally; return value=" + ret); ?
? ? ? ? ? ? return ret; ?
? ? ? ? } ?
? ? } ?
??
? ? public static void main(String[] args) { ?
? ?
? ? ? ? TestException testException1 = new TestException(); ?
? ? ? ? try { ?
? ? ? ? ? ? testException1.testEx(); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? }?
}
package com.imooc;
import javax.xml.transform.TransformerException;
public class TestException { ?
? ? public TestException() { ?
? ? } ?
??
? ? 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() throws Exception { ?
? ? ? ? boolean ret = true; ?
? ? ? ? try { ?
? ? ? ? ? ? ret = testEx2(); ?
? ? ? ? ? ? System.out.println("ret=="+ret);
? ? ? ? ? ? //System.out.println("echang=="+e);
? ? ? ? ? ? if (!ret) { ?
? ? ? ? ? ? ? ? return false; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? System.out.println("testEx1, at the end of try"); ?
? ? ? ? ? ? return ret; ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? System.out.println("testEx1游戲場(chǎng), catch exception"); ?
? ? ? ? ? ? ret = false; ?
? ? ? ? ? ? throw e; ?
? ? ? ? } finally { ?
? ? ? ? //System.out.println("異常是");
? ? ? ? ? ? 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 true; ?
? ? ? ? } /*catch (NullPointerException e) { ?
? ? ? ? ? ? System.out.println("testEx2低級(jí)異常, catch exception"); ?
? ? ? ? ? ? ret = false; ?
? ? ? ? ? ? //System.out.println("e=="+e);
? ? ? ? ? ? throw e; ?
? ? ? ? ? ? //System.out.println("能不能執(zhí)行?");
? ? ? ? } */finally { ?
? ? ? ? ? ? System.out.println("testEx2低級(jí)最終要執(zhí)行, finally; return value=" + ret); ?
? ? ? ? ? ? return ret; ?
? ? ? ? } ?
? ? } ?
??
? ? public static void main(String[] args) { ?
? ?
? ? ? ? TestException testException1 = new TestException(); ?
? ? ? ? try { ?
? ? ? ? ? ? testException1.testEx(); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? }?
}
2017-02-08
testEx2()會(huì)出異常,但是你把catch注釋了,它當(dāng)然捕獲不到,而且因?yàn)檫@是非檢查異常,所以系統(tǒng)不會(huì)自動(dòng)拋出,所以定義testEx2() 時(shí)后面的throws Exception等于沒起到作用,也即上一級(jí)的testEx1()和testEx()都不會(huì)捕獲到異常