posts - 241,  comments - 116,  trackbacks - 0
          定義三個異常類:ExceptionA,ExceptionB,ExceptionC

           
          ?
          public class ExceptionA extends Exception {
              public ExceptionA(String str) {
                  super();
              }
          }
           
          public class ExceptionB extends ExceptionA {
           
              public ExceptionB(String str) {
                  super(str);
              }
          }
           
          public class ExceptionC extends ExceptionA {
              public ExceptionC(String str) {
                  super(str);
              }
          }

          異常丟失的情況:

           
          ?
          public class NeverCaught {
              static void f() throws ExceptionB{
                  throw new ExceptionB("exception b");
              }
           
              static void g() throws ExceptionC {
                  try {
                      f();
                  } catch (ExceptionB e) {
                      ExceptionC c = new ExceptionC("exception a");
                      throw c;
                  }
              }
           
              public static void main(String[] args) {
                      try {
                          g();
                      } catch (ExceptionC e) {
                          e.printStackTrace();
                      }
              }
           
          }
          /*
          exception.ExceptionC
          at exception.NeverCaught.g(NeverCaught.java:12)
          at exception.NeverCaught.main(NeverCaught.java:19)
          */

          為什么只是打印出來了ExceptionC而沒有打印出ExceptionB呢?這個還是自己分析一下吧!

          上面的情況相當于少了一種異常,這在我們排錯的過程中非常的不利。那我們遇到上面的情況應該怎么辦呢?這就是異常鏈的用武之地:保存異常信息,在拋出另外一個異常的同時不丟失原來的異常。

           
          ?
          public class NeverCaught {
              static void f() throws ExceptionB{
                  throw new ExceptionB("exception b");
              }
           
              static void g() throws ExceptionC {
                  try {
                      f();
                  } catch (ExceptionB e) {
                      ExceptionC c = new ExceptionC("exception a");
                      //異常連
                      c.initCause(e);
                      throw c;
                  }
              }
           
              public static void main(String[] args) {
                      try {
                          g();
                      } catch (ExceptionC e) {
                          e.printStackTrace();
                      }
              }
           
          }
          /*
          exception.ExceptionC
          at exception.NeverCaught.g(NeverCaught.java:12)
          at exception.NeverCaught.main(NeverCaught.java:21)
          Caused by: exception.ExceptionB
          at exception.NeverCaught.f(NeverCaught.java:5)
          at exception.NeverCaught.g(NeverCaught.java:10)
          ... 1 more
          */

          這個異常鏈的特性是所有異常均具備的,因為這個initCause()方法是從Throwable繼承的。
          posted on 2011-12-26 10:25 墻頭草 閱讀(1557) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 辽宁省| 景宁| 萝北县| 麻阳| 吉木乃县| 化德县| 广丰县| 越西县| 彰化市| 开封市| 潞西市| 建德市| 汉川市| 合川市| 镇雄县| 崇阳县| 汤阴县| 雅安市| 荣成市| 五指山市| 黔南| 阜康市| 平塘县| 鄄城县| 新郑市| 棋牌| 阿勒泰市| 梁平县| 新巴尔虎左旗| 包头市| 宾阳县| 军事| 定日县| 驻马店市| 宿迁市| 修文县| 凤冈县| 德钦县| 高台县| 静乐县| 高淳县|