天天看點

深入了解try-catch-finally

深入了解try-catch-finally

1.下列代碼的運作結果是:

public class TryTest01 {

       static int test(){

              int x=1;

              try {

                     return x;

              }finally{

                     ++x;

              }

       }

       public static void main(String[] args) {

              System.out.println(test());

       }

}

2.下列代碼的運作結果是:

public class TryTest02 {

       public static void main(String[] args) {

              System.out.println(new TryTest02().get());

       }

       static int get() {

              try {

                     return 1;

              } finally {

                     return 2;

              }

       }

}

3.下列代碼的運作結果是:

public class TryTest03 {

   static int get(){

          int i=3;

          try {

                 return i;

       } finally{

              System.out.println("aa");

       }

   }

       public static void main(String[] args) {

              System.out.println("結果是:"+newTryTest03().get());

       }

}

運作結果:

(1)1       (2)2   

(3)aa

     結果是:3

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

深入了解try-catch-finally

本内容由安康學院"雨季"原創!

繼續閱讀