Nathan-Yang 2017-07-14 08:52 采纳率: 0%
浏览 2194
已采纳

java 异常和return的执行

public class Test1{

public String testt() throws Exception{
    try {
        int i = 9/0;
    } catch (Exception e) {
        throw e;
    }finally{
        return "test1";
    }
}

}


public class Test2 {
    public static void main(String[] args) {
        Test1 t = new Test1();
        String str = "test2";
        try{
            str = t.testt();
            System.out.println(str);
        }catch(Exception e){
            System.out.println(str);
        }

    }
}

上述代码输出“test1”,有没有大神出来解释下为什么Test2里面的catch没有进去?

  • 写回答

5条回答 默认 最新

  • IAmObject 2017-07-14 09:31
    关注

    你的testt()方法写了finally的,finally是无论如何都会执行的代码块,对于Test2 中的str 得到值的过程并没有出现异常,所以不会进入到catch代码块里面的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?