深入剖析Java中的各種異常處理方式_java

來源:互聯網
上載者:User

1. 調試追蹤代碼:

  public static void enterTryMethod() {     System.out.println("enter after try field");   }      public static void enterExceptionMethod() {     System.out.println("enter catch field");   }      public static void enterFinallyMethod() {     System.out.println("enter finally method");   } 

2. 拋出Exception,沒有finally,當catch遇上return

   

public static int catchTest() {     int res = 0;          try {       res = 10 / 0; // 拋出Exception,後續處理被拒絕       enterTryMethod();       return res; // Exception已經拋出,沒有獲得被執行的機會     } catch (Exception e) {       enterExceptionMethod();       return 1;  // Exception拋出,獲得了調用方法並返回方法值的機會     }   } 

後台輸出結果:

  enter catch field   1 

3. 拋出Exception,當catch體裡有return,finally體的代碼塊將在catch執行return之前被執行

   

public static int catchTest() {     int res = 0;          try {       res = 10 / 0; // 拋出Exception,後續處理被拒絕       enterTryMethod();       return res; // Exception已經拋出,沒有獲得被執行的機會     } catch (Exception e) {       enterExceptionMethod();       return 1;  // Exception拋出,獲得了調用方法並返回方法值的機會     } finally {       enterFinallyMethod(); // Exception拋出,finally代碼將在catch執行return之前被執行     }   } 

後台輸出結果:

  enter catch field   enter finally method   1 

4. 不拋出Exception,當finally代碼塊裡面遇上return,finally執行完後將結束整個方法

  public static int catchTest() {     int res = 0;          try {       res = 10 / 2; // 不拋出Exception       enterTryMethod();       return res; // 獲得被執行的機會,但執行需要在finally執行完成之後才能被執行     } catch (Exception e) {       enterExceptionMethod();       return 1;     } finally {       enterFinallyMethod();       return 1000; // finally中含有return語句,這個return將結束這個方法,不會在執行完之後再跳回try或者catch繼續執行,方法到此結束     }   } 

後台輸出結果:

  enter after try field   enter finally method   1000 

5. 不拋Exception,當finally代碼塊裡面遇上System.exit()方法將結束和終止整個程式,而不只是方法

 

  public static int catchTest() {     int res = 0;          try {       res = 10 / 2; // 不拋出Exception       enterTryMethod();       return res; // 獲得被執行的機會,但由於finally已經終止程式,傳回值沒有機會被返回     } catch (Exception e) {       enterExceptionMethod();       return 1;     } finally {       enterFinallyMethod();       System.exit(0); // finally中含有System.exit()語句,System.exit()將退出整個程式,程式將被終止     }   } 

後台輸出結果:

  enter after try field   enter finally method 

6. 拋出Exception,當catch和finally同時遇上return,catch的return傳回值將不會被返回,finally的return語句將結束整個方法並返回

   

 public static int catchTest() {     int res = 0;          try {       res = 10 / 0; // 拋出Exception,後續處理將被拒絕       enterTryMethod();       return res; // Exception已經拋出,沒有獲得被執行的機會     } catch (Exception e) {       enterExceptionMethod();       return 1; // Exception已經拋出,獲得被執行的機會,但返回操作將被finally截斷     } finally {       enterFinallyMethod();       return 10; // return將結束整個方法,傳回值為10     }   } 

後台輸出結果:

  enter catch field   enter finally method   10 

7. 不拋出Exception,當finally遇上return,try的return傳回值將不會被返回,finally的return語句將結束整個方法並返回

   

 public static int catchTest() {     int res = 0;          try {       res = 10 / 2; // 不拋出Exception       enterTryMethod();       return res; // 獲得執行機會,但返回將被finally截斷     } catch (Exception e) {       enterExceptionMethod();       return 1;     } finally {       enterFinallyMethod();       return 10; // return將結束整個方法,傳回值為10     }   } 

後台輸出結果:

  enter after try field   enter finally method   10 


結論
Java的異常處理中,程式執行完try裡面的代碼塊之後,該方法並不會立即結束,而是繼續試圖去尋找該方法有沒有finally的代碼塊

    如果沒有finally代碼塊,整個方法在執行完try代碼塊後返回相應的值來結束整個方法
    如果有finally代碼塊,此時程式執行到try代碼塊裡的return一句之時並不會立即執行return,而是先去執行finally代碼塊裡的代碼

若finally代碼塊裡沒有return或沒有能夠終止程式的代碼,程式在執行完finally代碼塊代碼之後再返回try代碼塊執行return語句來結束整個方法。若 finally 代碼塊裡有 return 或含有能夠終止程式的代碼,方法將在執行完 finally 之後被結束,不再跳回 try 代碼塊執行 return
在拋出異常的情況下,原理也是和上面的一樣的,你把上面說到的 try 換成 catch 去理解就OK了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.