java異常處理-finally中使用return和throw語句


java異常語句中的finally塊通常用來做資源釋放操作,如關閉文件、關閉網絡連接、關閉數據庫連接等。正常情況下finally語句中不應該使用return語句也不應該拋出異常,以下討論僅限於java語言設計本身,正常編碼時應避免。

finally塊中使用return會覆蓋method的返回值

以下代碼的返回值為:1

public static int div(){
        try {
            return 3;
        }catch (ArithmeticException e){
            System.out.println("catch in div");
            return 2;
        }
        finally {
            System.out.println("finally in div");
            return 1;
        }
    }

以下代碼的返回值同樣是:1

 public static int div(){
        try {
            return 3/0;
        }catch (ArithmeticException e){
            System.out.println("catch in div");
            return 2;
        }
        finally {
            System.out.println("finally in div");
            return 1;
        }
    }

 

finally塊中使用return會抑制異常的冒泡傳輸

即:只要finally中使用了return語句,調用者便認為該方法正常返回

以下代碼的輸出為

catch in div
finally in div
catch in adapter
finally in adapter

/**
 * Created by Administrator on 2017/11/27.
 */

public class Test {

    public static void main(String[] args) {
        adapter();
    }

    public static void adapter() {
        try {
            div();
        } catch (ArithmeticException e) {
            System.out.println("catch in adapter");
        } finally {
            System.out.println("finally in adapter");
        }
    }

    public static int div() {
        try {
            int a = 5 / 0;
            return a;
        } catch (ArithmeticException e) {
            System.out.println("catch in div");
            throw e;  // 重新將異常拋出給調用者
        } finally {
            System.out.println("finally in div");
        }
    }
}

 

 但如果在 div 的finally塊中添加了return語句

    public static int div(){
        try {
            int a =  5/0;
            return a;
        }catch (ArithmeticException e){
            System.out.println("catch in div");
            throw  e; // 重新將異常拋出給調用者,但是拋出會被忽略
        }
        finally {
            System.out.println("finally in div");
            return 1;
        }
    }

則代碼的輸出為

catch in div
finally in div
finally in adapter

即:finally塊中的return語句會阻止異常的棧調用傳輸,使caller認為該方法已經正常返回

 

finally塊中的throw語句會覆蓋try和catch語句中的異常

以下代碼

/**
 * Created by Administrator on 2017/11/27.
 */

public class Test {

    public static void main(String[] args) {
        adapter();
    }

    public static void adapter() {
        try {
            div();
        } catch (Exception e) {
            System.out.println(String.format("catch in adapter: %s",e.getMessage()));
        } finally {
            System.out.println("finally in adapter");
        }
    }

    public static int div() throws Exception{
        try {
            int a = 5 / 0;
            return a;
        } catch (ArithmeticException e) {
            System.out.println("catch in div");
            throw new Exception("Exception in div"); // 拋出新的異常
        } finally {
            System.out.println("finally in div");
            throw new Exception("Exception in Finally");  // 拋出新的異常
        }
    }
}

輸出是:

catch in div
finally in div
catch in adapter: Exception in Finally
finally in adapter

即,catch塊中拋出的異常北finally塊拋出的異常替換了

 

修改div方法為

 public static int div() throws Exception{
        try {
            int a = 5 / 0;
            return a;
        }  finally {
            System.out.println("finally in div");
            throw new Exception("Exception in Finally");  // 拋出新的異常
        }
    }

輸出為:

finally in div
catch in adapter: Exception in Finally
finally in adapter

即,try塊中捕獲的異常北finally塊拋出的異常替換

 

finally塊和普通代碼塊一樣,無法同時使用return語句和throw語句,因為無法通過編譯


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com