人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

對于java異常處理機制的深入理解

對于java異常處理機制的深入理解

更新時間:2022-07-28 文章作者:未知 信息來源:網絡 閱讀次數:

1 引子

  try…catch…finally恐怕是大家再熟悉不過的語句了,而且感覺用起來也是很簡單,邏輯上似乎也是很容易理解。不過,我親自體驗的“教訓”告訴我,這個東西可不是想象中的那么簡單、聽話。不信?那你看看下面的代碼,“猜猜”它執行后的結果會是什么?不要往后看答案、也不許執行代碼看真正答案哦。如果你的答案是正確,那么這篇文章你就不用浪費時間看啦。

  package myExample.testException;

  public class TestException {

   public TestException() {

   }

   boolean testEx() throws Exception{

   boolean ret = true;

   try{

   ret = testEx1();

   }catch (Exception e){

   System.out.println("testEx, catch exception");

   ret = false;

   throw e;

   }finally{

   System.out.println("testEx, finally; return value="+ret);

   return ret;

   }

   }

   boolean testEx1() throws Exception{

   boolean ret = true;

   try{

   ret = testEx2();

   if (!ret){

   return false;

   }

  System.out.println("testEx1, at the end of try");

   return ret;

   }catch (Exception e){

   System.out.println("testEx1, catch exception");

   ret = false;

   throw e;

   }

   finally{

   System.out.println("testEx1, finally; return value="+ret);

   return ret;

   }

   }


   boolean testEx2() throws Exception{

   boolean ret = true;

   try{

   int b=12;

   int c;

   for (int i=2;i>=-2;i--){

   c=b/i;

   System.out.println("i="+i);

   }

   return true;

   }catch (Exception e){

   System.out.println("testEx2, catch exception");

   ret = false;

   throw e;

   }

   finally{

   System.out.println("testEx2, finally; return value="+ret);

   return ret;

   }

   }


   public static void main(String[] args) {

   TestException testException1 = new TestException();

   try{

   testException1.testEx();

   }catch(Exception e){

   e.printStackTrace();

   }

   }

  }


  你的答案是什么?是下面的答案嗎?

  i=2

  i=1

  testEx2, catch exception

  testEx2, finally; return value=false

  testEx1, catch exception

  testEx1, finally; return value=false

  testEx, catch exception

  testEx, finally; return value=false


  如果你的答案真的如上面所說,那么你錯啦。^_^,那就建議你仔細看一看這篇文章或者拿上面的代碼按各種不同的情況修改、執行、測試,你會發現有很多事情不是原來想象中的那么簡單的。

  現在公布正確答案:

  i=2

  i=1

  testEx2, catch exception

  testEx2, finally; return value=false

  testEx1, finally; return value=false

  testEx, finally; return value=false

  2 基礎知識
  2.1 相關概念
  例外是在程序運行過程中發生的異常事件,比如除0溢出、數組越界、文件找不到等,這些事件的發生將阻止程序的正常運行。為了加強程序的魯棒性,程序設計時,必須考慮到可能發生的異常事件并做出相應的處理。C語言中,通過使用if語句來判斷是否出現了例外,同時,調用函數通過被調用函數的返回值感知在被調用函數中產生的例外事件并進行處理。全程變量ErroNo常常用來反映一個異常事件的類型。但是,這種錯誤處理機制會導致不少問題。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 渑池县| 武山县| 玉门市| 定安县| 盖州市| 长丰县| 彭阳县| 永川市| 拜城县| 田林县| 苗栗县| 喀什市| 长海县| 弥渡县| 光山县| 永昌县| 新昌县| 黔西县| 奉化市| 万盛区| 宁城县| 临沂市| 西和县| 呼图壁县| 西昌市| 石首市| 绥江县| 扶余县| 雷州市| 西充县| 金塔县| 海南省| 丹东市| 旬邑县| 江华| 淮安市| 全椒县| 隆化县| 延安市| 龙南县| 屏边|