java中关于try、catch、finally中的细节分析

Demo1

public class Demo1 {
    public static final String test() {
        String t = "";

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

    public static void main(String[] args) {
        System.out.print(Demo1.test());
    }
}

Demo2

public class Demo2 {
    public static final String test() {
        String t = "";

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

    public static void main(String[] args) {
        System.out.print(Demo2.test());
    }

Demo3

public class Demo3 {
    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";
            // System.out.println(t);
            // return t;
        }
    }

    public static void main(String[] args) {
        System.out.print(Demo3.test());
    }

}

Demo4

public class Demo4 {
    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(Demo4.test());
    }
}

Demo5

/**
 * java.lang.NumberFormatException: null
 */
public class Demo5 {

    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(Demo5.test());
    }
}

Demo6

public class Demo6 {
    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(Demo6.test());
    }

}

Demo7

/**
 * java.lang.NumberFormatException: null
 */
public class Demo7 {

    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(Demo7.test());
    }
}

Demo8

public class Demo8 {
    @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(Demo8.test());
    }
}

Demo9

public class Demo9 {
    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(Demo9.test());
    }
}

对以上全部的例子进行总结

  • 1 try、catch、finally语句中,在若是try语句有return语句,则返回的以后当前try中变量此时对应的值,此后对变量作任何的修改,都不影响try中return的返回值
  • 2 若是finally块中有return 语句,则返回try或catch中的返回语句忽略。
  • 3 若是finally块中抛出异常,则整个try、catch、finally块中抛出异常

因此使用try、catch、finally语句块中须要注意的是

  • 1 尽可能在try或者catch中使用return语句。经过finally块中达到对try或者catch返回值修改是不可行的。
  • 2 finally块中避免使用return语句,由于finally块中若是使用return语句,会显示的消化掉try、catch块中的异常信息,屏蔽了错误的发生
  • 3 finally块中避免再次抛出异常,不然整个包含try语句块的方法回抛出异常,而且会消化掉try、catch块中的异常
相关文章
相关标签/搜索