try-catch-finally问题

try-catch-finally问题

9个例子,弄清楚try-catch-finally-return

例1

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {        
            t = "catch";
            return t;
        } finally {
            t = "finally";
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回结果:try

例2

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {        
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:finally

例3

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";         
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:catch

例4

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:finally

例5

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            Integer.parseInt(null);
            return t;
        } finally {
            t = "finally";     
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:
Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at com.it.test.TryCatchFinally.test(TryCatchFinally.java:20)
    at com.it.test.TryCatchFinally.main(TryCatchFinally.java:29)

例6

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (Exception e) {
            t = "catch";
            Integer.parseInt(null);
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:
finally

例7

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:
Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at com.it.test.TryCatchFinally.test(TryCatchFinally.java:16)
    at com.it.test.TryCatchFinally.main(TryCatchFinally.java:27)

例8

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            Integer.parseInt(null);
            return t;
        } catch (NullPointerException e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:finally

例9

public class TryCatchFinally {
    @SuppressWarnings("finally")
    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            t = "catch";
            return t;
        } finally {
            t = "finally";
            String.valueOf(null);
            return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(TryCatchFinally.test());
    }
}
返回值:
Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.(String.java:168)
    at java.lang.String.valueOf(String.java:2861)
    at com.it.test.TryCatchFinally.test(TryCatchFinally.java:21)
    at com.it.test.TryCatchFinally.main(TryCatchFinally.java:27)

结论:


finally前语句(try,catch)中有return,那么会先保存临时值,finlly结束后会返回临时值,finally中的return优先前语句中的return

  1. try、catch、finally语句中,如果只有try语句有return返回值,此后在catch、finally中对变量做任何的修改,都不影响try中return的返回值。

  2. try、catch中有返回值,而try中抛出的异常恰好与catch中的异常匹配,则返回catch中的return值。

  3. 如果finally块中有return 语句,则返回try或catch中的返回语句忽略。

  4. 如果finally块中抛出异常,则整个try、catch、finally块中抛出异常.并且没有返回值

    所以在使用try、catch、finally语句块时需要注意以下几点:

    • 尽量在try或者catch中使用return语句。通过finally块中达到对try或者catch返回值修改是不可行的。
    • finally块中避免使用return语句,因为finally块中如果使用return语句,会显示的忽略掉try、catch块中的异常信息,屏蔽了错误的发生。
    • finally块中避免再次抛出异常,否则整个包含try语句块的方法回抛出异常,并且会忽略掉try、catch块中的异常。

    原文链接:https://blog.csdn.net/mxd446814583/article/details/80355572

你可能感兴趣的:(try-catch-finally问题)