码迷,mamicode.com
首页 > 编程语言 > 详细

java 異常抛出 throw 與 return

时间:2017-03-06 01:31:33      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:int   on()   out   for   false   final   dex   string   index   

package 異常;  
 
public class TestException {  
    public TestException() {  
    }  
 
    boolean testEx() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx1();  
        } catch (Exception e) {  
            e.printStackTrace();
            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();  
            if (!ret) {  
                return false;  
            }  
            System.out.println("testEx1, at the end of try");  
            return ret;  
        } catch (Exception e) {  
            e.printStackTrace();
            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 true;  
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("testEx2, catch exception");  
            ret = false;  
           //極其重要 throw 可以代替 return 若後面 finally 中是 return 調用此方法的將不會收到異常
           //(即catch裏面作爲結果抛出的異常被finally裏面的return覆蓋了)   若沒有return也沒有抛出某異常  則調用的將會接到此處抛出的異常並處理
            throw e;    
        } finally {  
            System.out.println("testEx2, finally; return value=" + ret);  
//            return ret;  
            throw new ArrayIndexOutOfBoundsException();
        }
//        System.out.println("執行中");
//        return false;
    }  
 
    public static void main(String[] args) {  
        TestException testException1 = new TestException();  
        try {  
            testException1.testEx();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("main");
        }  
    }  
}

 

結果

i=2
i=1
java.lang.ArithmeticException: / by zero
    at 異常.TestException.testEx2(TestException.java:48)
    at 異常.TestException.testEx1(TestException.java:25)
    at 異常.TestException.testEx(TestException.java:10)
    at 異常.TestException.main(TestException.java:71)
testEx2, catch exception
testEx2, finally; return value=false
java.lang.ArrayIndexOutOfBoundsException
    at 異常.TestException.testEx2(TestException.java:62)
    at 異常.TestException.testEx1(TestException.java:25)
    at 異常.TestException.testEx(TestException.java:10)
    at 異常.TestException.main(TestException.java:71)
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false

   質料   http://www.cnblogs.com/to-creat/p/5688235.html

java 異常抛出 throw 與 return

标签:int   on()   out   for   false   final   dex   string   index   

原文地址:http://www.cnblogs.com/LiuPan2016/p/6507590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!