Java语言中try-catch-finally的执行顺序

第一种情况:try中有return                                                    

private int testTryReturn(){                                                     

    int num =10 ;                                                             

    Log.i("test","----- testTryReturn start -----");                                     

    try {                                                                    

        Log.i("test","----- try ----- ");

        return num+=2;

    }catch (Exception e){

        num+=3;

        Log.i("test","----- Exception ----- ");

    }finally {

        num+=4;

        Log.i("test","----- finally ----- num="+num);

    }

    Log.i("test","----- testTryReturn end ----- num="+num);

    return num;

}

打印的结果

com.example.android.myapplication I/test: ----- testTryReturn start -----

com.example.android.myapplication I/test: ----- try -----

com.example.android.myapplication I/test: ----- finally ----- num=16

testTryReturn() 的返回值:12

    

第二种情况:tryfinally中有return                                            

private int testTryAndfinallyReturn(){

    int num =10 ;

    Log.i("test","----- testTryReturn start -----");

    try {

        Log.i("test","----- try ----- ");

        return num+=2;

    }catch (Exception e){

        num+=3;

        Log.i("test","----- Exception ----- ");

    }finally {

        num+=4;

        Log.i("test","----- finally ----- num="+num);

        return num+=1;

    }

}

打印的结果

com.example.android.myapplication I/test: ----- testTryReturn start -----

com.example.android.myapplication I/test: ----- try -----

com.example.android.myapplication I/test: ----- finally ----- num=16

testTryReturn() 的返回值:17

 

第三种情况:try中有return,并且将num的值包装在Num类中                     

private Num testTryReturnWithClassNum(){

Num num = new Num();

Log.i("test","----- testTryReturn start -----");

    try {

        Log.i("test","----- try ----- ");

        return num;

    }catch (Exception e){

        num.num+=3;

        Log.i("test","----- Exception ----- ");

    }finally {

        num.num+=4;

        Log.i("test","----- finally ----- num="+num.num);

    }

    Log.i("test","----- testTryReturn end ----- num="+num.num);

    return num;

}

打印的结果

com.example.android.myapplication I/test: ----- testTryReturn start -----

com.example.android.myapplication I/test: ----- try -----

com.example.android.myapplication I/test: ----- finally ----- num=156

testTryReturnWithClassNum().num 的返回值:156

 

class Num{

    public int num = 152;

}

 

第四种情况:抛出Exception,catch体里有return                                

private int testCatch() {

    int num = 20;

    Log.i("test","----- testTryReturn start -----");

    try {

        Log.i("test","----- try ----- ");

        num = 10/0;

        Log.i("test","----- try ----- num="+num);

        return num+=1;

    } catch (Exception e) {

        num+=3;

        Log.i("test","----- Exception ----- num="+num);

        return num;

    } finally {

        num+=4;

        Log.i("test","----- finally ----- num="+num);

    }

}

打印的结果

com.example.android.myapplication I/test: ----- testTryReturn start -----

com.example.android.myapplication I/test: ----- try -----

com.example.android.myapplication I/test: ----- Exception ----- num=23

com.example.android.myapplication I/test: ----- finally ----- num=27

testCatch() 的返回值:23

 

第五种情况: 抛出Exception,catch体里有return,finally里有return               

private int testCatchAndFinallyReturn(){

    int num = 20;

    Log.i("test","----- testTryReturn start -----");

    try {

        Log.i("test","----- try ----- ");

        num = 10/0;

        Log.i("test","----- try ----- num="+num);

        return num+=1;

    }catch (Exception e){

        num+=3;

        Log.i("test","----- Exception ----- num="+num);

        return num;

    }finally {

        num+=4;

        Log.i("test","----- finally ----- num="+num);

        return num;

    }

}

打印的结果

com.example.android.myapplication I/test: ----- testTryReturn start -----

com.example.android.myapplication I/test: ----- try -----

com.example.android.myapplication I/test: ----- Exception ----- num=23

com.example.android.myapplication I/test: ----- finally ----- num=27

testCatchAndFinallyReturn() 的返回值:27

 

第六种情况:抛出Exception,catch体里有return,finally里有return,并且执行System.exit(0);

private int testCatchAndFinallyReturnWithSystemExit(){

    int num = 20;

    Log.i("test","----- testTryReturn start -----");

    try {

        Log.i("test","----- try ----- ");

        num = 10/0;

        Log.i("test","----- try ----- num="+num);

        return num+=1;

    }catch (Exception e){

        num+=3;

        Log.i("test","----- Exception ----- num="+num);

        return num;

    }finally {

        num+=4;

        Log.i("test","----- finally ----- num="+num);

        System.exit(0);

        return num;

    }

}

打印的结果

com.example.android.myapplication I/test: ----- testTryReturn start -----

com.example.android.myapplication I/test: ----- try -----

com.example.android.myapplication I/test: ----- Exception ----- num=23

com.example.android.myapplication I/test: ----- finally ----- num=27

程序退出

    

总结                                                                          

--------------------------------------------------------------------------------------------------------------------------------

    Java的异常处理中,程序执行完try里面的代码块之后,该方法并不会立即结束,而是继续试图去寻找该方法有没有finally的代码块

     ** 如果没有finally代码块,整个方法在执行完try代码块后返回相应的值来结束整个方法

     ** 如果有finally代码块,此时程序执行到try代码块里的return一句之时并不会立即执行return,而是先去执行finally代码块里的代码

    finally代码块里没有return或没有能够终止程序的代码,程序在执行完finally代码块代码之后再返回try代码块执行return语句来结束整个方法。

    finally代码块里有return或含有能够终止程序的代码,方法将在执行完finally之后被结束,不再跳回try代码块执行return

    在抛出异常的情况下,原理也是和上面的一样的;

    

---------------------------------------------------------------------------------------------------------------------------------

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页