JAVA异常处理机制

         引子
try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解。不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单、听话。不信?那你看看下面的代码,“猜猜”它执行后的结果会是什么?不要往后看答案、也不许执行代码看真正答案哦。如果你的答案是正确,那么这篇文章你就不用浪费时间看啦。

package myExample.testException;
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();
            if (!ret){
                return false;
                       
System.out.println("testEx1, at the end of try");
            return ret;
        }catch (Exception e){
     
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章