try catch finally 正確使用方法__java

來源:互聯網
上載者:User
public class AAA {  
  
    public static void main(String[] args) {  
    System.out.println("=============test0==================");  
         System.out.println(test0());  
         System.out.println("===============================");  
         
         System.out.println("=============test0_1==================");  
         System.out.println(test0_1());  
         System.out.println("===============================");  
         System.out.println("=============test0_2==================");  
         System.out.println(test0_2());  
         System.out.println("===============================");  
         
        System.out.println("=============test1==================");  
        System.out.println(test1());  
        System.out.println("===============================");  
  
        System.out.println("=============test1_1==================");  
        System.out.println(test1_1());  
        System.out.println("===============================");  
  
        System.out.println("\n============test2===================");  
        System.out.println(test2());  
        System.out.println("===============================");  
  
        System.out.println("\n============test2_1===================");  
        System.out.println(test2_1());  
        System.out.println("===============================");  
  
        System.out.println("\n============test3===================");  
        System.out.println(test3());  
        System.out.println("===============================");  
  
        System.out.println("\n============test3_1===================");  
        System.out.println(test3_1());  
        System.out.println("===============================");  
    }  
  
    public static String test0() {  
        String a;  
        int b=0;  
        try{  
            b = 8/0;  //(1)  ------0不能當被除數,此行出現異常,try裡面的return不會執行
            return "try";    //=========================沒有執行======================================
        }catch(Exception e){  
            e.printStackTrace();//(2) //此時程式不會終止,還會繼續往下執行
        }  
        a = String.valueOf(b);  //(3)
        return a+b;  //(4)
    }  
    
    
    //try和catch中都有了return
    public static String test0_1() {  
        String a;  
        int b;  
        try{  
            b = 8/0;    //(1) 0不能當被除數,此行出現異常,try裡面的return不會執行
            a = String.valueOf(b);  //===================不執行
            return a+b;             //===================不執行
        }catch(Exception e){ 
        e.printStackTrace(); //(2)
            return "test0_1 出錯了";  //(3)
        }  
       // String s="zhaohao";             //try和catch中都有了return,此處加 任何東西  會報錯     --------不讓加,報錯
       // System.out.println("哈哈 ,趙皓");  //try和catch中都有了return,此處加 任何東西  會報錯    --------不讓加,報錯
       //return a+b;                     //try和catch中都有了return,此處加return會報錯        --------不讓加,報錯
    }  
      
    public static String test0_2() {  
        String a;  
        int b=0;  
        try{  
            b = 8/0;  
        }catch(Exception e){  
        b=100;
        }  
        a = String.valueOf(b);     //會執行此處===================== b為100
        return a;                  //會執行此處=====================
    }  
      
    
    
    //=============================================================
    //==========總結:try裡面的return會在finnally執行之後執行================
    //============================================================= 
    public static int test1() {  
        int  m = 3;
        int  n = 2; 
        int  a = 7;  
        int  b = 8; 
        
        int s =0;
        String  str="";
        try{ 
        s=m+n; //(1)
        str="我是try中的字串";
            return s; //(2)先執行這個===(此時s的值已經確認,finally並不會修改它),====再執行finally  ===================(5)結束了,此時才會真正的返回該值
        } catch ( Exception e ) {  
  
        } finally {  
            //===========對try中int和String的更改均無效  =================================?????????????????????????????
           s=a+b;   //(3)
           str="我是finally中的字串";
           System.out.println("do finally 哈哈");  //(4)
        }  
        return s; //==============不會執行===========  。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    } 
    
    //=============================================================
    //==========總結:try裡面的return會在finnally執行之後執行================
    //=============================================================
    
   //執行結果:
   // do finally 哈哈           
   // 5
    
    
    
  
    
    
    public static String test1_1() {  
        String a = "in try中";  
  
        try{  
            return a;    //因為finnally中有return,所以該return不會執行
        } catch ( Exception e ) {  
  
        } finally {   //從eclpise警示告可看出,finally裡面不建議有return語句  
            a = "in finally中的return的值";  
            System.out.println("test1_1 中的  do finally");  
            return a; //注釋掉這句,eclipse將不再警告  ================================
        }  
    }  
  
    //返回結果:
    //test1_1 中的  do finally
    //in finally中的return的值
    
    
    /** 
     * 總結: 
     * return語句,finally裡面不建議放return語句,根據需要,可以放在try和catch裡面 
     *  
     */  
      
    public static int test2() {  
        int a = 1;  
  
        try{  
            return a;  
        } catch ( Exception e ) {  
  
        } finally {  
            a = 2;  
            System.out.println("do finally");  
        }  
  
        return a;  
    } //很顯然,finally裡面更改無效,返回的是a=1  
  
    
    
    /** 
     * 總結: 
     * return語句,finally裡面不建議放return語句,根據需要,可以放在try和catch裡面 
     *  
     */  
      
    public static int test2_1() {  
        int a = 1;  
  
        try{  
            return a;  
        } catch ( Exception e ) {  
  
        } finally {  
            a = 222222222;  
            System.out.println("do finally   test2_1");  
            return a;  
        }  
    } //很顯然,a取finally裡面的值,a=2222222222  
  
    
    
    //Helper類,將整數轉換成字串  
    static class Helper {  
        int a;  
  
        public String toString() {  
            return String.valueOf(a);  
        }  
    }  
      
    public static Helper test3() {  
        Helper h = new Helper();  
        h.a = 1;  
  
        try{  
            return h;  
        } catch ( Exception e ) {  
  
        } finally {  
            h.a = 2; //對h.a的更改起作用!!  
                    //因為在try裡面返回的是一個控制代碼,它指向的對象的內容 是可以改變的  
            System.out.println("do finally");  
        }  
          
        return h; //這個不會被執行  
    }  
  
    public static Helper test3_1() {  
        Helper h = new Helper();  
        h.a = 1;  
  
        try{  
            return h;  
        } catch ( Exception e ) {  
  
        } finally {  
            h.a = 2; //返回a=2,這個不用說了  
            System.out.println("do finally");  
            return h;  
        }  
    }  
  
  
    /** 
     * 總結: 
     * return語句,finally裡面不建議放return語句,根據需要,可以放在try和catch裡面 
     *  
     */  
      
}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.