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塊中的異常
相關文章
相關標籤/搜索