1
Java基础系列

本文主要简述try-catch-finally中有return的情况。笔试面试经常考到。

题目

/**
 * http://qing0991.blog.51cto.com/1640542/1387200
 * Created by codecraft on 2016-03-08.
 */
public class ReturnDemo {

    /**
     * try有return,finally也有return,以finally的为准
     * @return
     */
    public static int noException(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i--; //9
            return --i; //8
        }
        catch(Exception e){
            --i;
            System.out.println("i in catch - form try block is:"+i);
            return --i;
        }
        finally{
            System.out.println("i in finally - from try or catch block is:"+i); //8
            return ++i; //9 返回这个
        }
    }

    public static int tryCatchReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            return --i; //9
        }
        catch(Exception e){
            --i;
            System.out.println("i in catch - form try block is:"+i);
            return --i;
        }
        finally{
            System.out.println("i in finally - from try or catch block is:"+i); //9
            --i; //8
            System.out.println("i in finally block is:"+i); //8
        }
    }

    /**
     * finally的return的优先级最高
     * 但try/catch的return也会执行
     * @return
     */
    public static int tryCatchFinallyReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i = i/0;
            return --i;
        }
        catch(Exception e){
            System.out.println("i in catch - form try block is:"+i); //10
            --i; //9
            System.out.println("i in catch block is:"+i); //9
            return --i; //8
        }
        finally{
            System.out.println("i in finally - from try or catch block is--"+i); //8
            --i; //7
            System.out.println("i in finally block is--"+i); //7
            return --i; //6
        }
    }

    public static int tryExCatchReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i=i/0;
            return --i;
        }catch(Exception e){
            System.out.println("i in catch - form try block is:"+i); //10
            return --i; //9 返回这个
        }finally{

            System.out.println("i in finally - from try or catch block is:"+i); //9
            --i; //8
            System.out.println("i in finally block is:"+i); //8
        }
    }

    public static int tryExCatchExReturn() {
        int i = 10;
        try {
            System.out.println("i in try block is:" + i); //10
            i = i / 0;
            return --i;
        } catch (Exception e) {
            System.out.println("i in catch - form try block is:" + i); //10
            int j = i / 0; 
            return --i;
        } finally {

            System.out.println("i in finally - from try or catch block is:" + i); //10
            --i; //9
            --i; //8
            System.out.println("i in finally block is:" + i); //8
            return --i; //7 返回这个
        }
    }


    public static void main(String[] args){
        System.out.println("###" + noException()); //9
        System.out.println("###" + tryCatchReturn()); //7 
        System.out.println("###" + tryCatchFinallyReturn()); //6
        System.out.println("###" + tryExCatchReturn()); //9 
        System.out.println("###" + tryExCatchExReturn()); //7
    }
}

小结

如果finally中有return,则最后的返回以它为准,同时try或catch的return如果可以运行到,也会执行其表达式。

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...