Java異常處理場景中不同位置的返回值詳細解析

笑江湖2100發表於2021-01-23

Java 異常處理中的返回值在不同位置不同場景下是有一些差別的,這裡需要格外注意

具體分以下兩種場景:

  • 1 finally語句塊沒有return語句,即當程式碼執行到try或者catch語句塊中的return語句時,相當於在本地儲存了一份此時返回值的變數的值副本,然後按照try catch finally的執行機制去執行finally語句塊,然後回過來將剛剛儲存的副本資料返回

第一個例子為在try中沒有異常,直接走到try語句塊中的return場景

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
        }
    }
}

執行結果如下,最後函式返回的值是1,而不是2

in try.....
results = 1
in finally
results = 2
func results:1

第二個例子為try中出現了異常,但是在catch中走到了return語句

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            double d=1/0;
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
        }
    }
}

返回值如下,即此時返回值是2而不是3

in try.....
results = 1
in catch......
results = 2
in finally
results = 3
func results:2
  • 2 finally語句塊有return語句,此時即最終返回finally語句塊中的的值,而try或者catch語句塊中及時有return語句,也不會發生作用

下面的例子即try中能走到return語句,同樣finally中也有return語句時候,此時返回finally語句塊中的return的值

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
    }
}

執行結果如下,返回2,此時不是返回1了

in try.....
results = 1
in finally
results = 2
func results:2

下面的例子即在catch走到了return語句,同樣在finally語句塊中也走到了return語句,此時同樣也是返回finally語句塊中的值,catch語句塊中的返回值失效

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            double d=1/0;
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
    }
}

執行結果如下:返回3而不是2了

in try.....
results = 1
in catch......
results = 2
in finally
results = 3
func results:3

相關文章