Swing


          天行健 君子以自強不息

          posts - 69, comments - 215, trackbacks - 0, articles - 16
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          Java提供了兩類主要的異常:runtime exception和checked exception。所有的checked exception是從java.lang.Exception類衍生出來的,而runtime exception則是從java.lang.RuntimeException或java.lang.Error類衍生出來的。

          它們的不同之處表現(xiàn)在兩方面:機制上和邏輯上。

          一、機制上

          它們在機制上的不同表現(xiàn)在兩點:1.如何定義方法;2. 如何處理拋出的異常。請看下面CheckedException的定義:


          public class CheckedException extends Exception
          {
           public CheckedException() {}
           public CheckedException( String message )
           {
            super( message );
           }
          }




          以及一個使用exception的例子:


          public class ExceptionalClass
          {
           public void method1()
            throws CheckedException
            {
             // ... throw new CheckedException( "...出錯了" );
            }
           public void method2( String arg )
            {
             if( arg == null )
             {
              throw new NullPointerException( "method2的參數(shù)arg是null!" );
             }
            }
           public void method3() throws CheckedException
            {
             method1();
            }
          }




          你可能已經(jīng)注意到了,兩個方法method1()和method2()都會拋出exception,可是只有method1()做了聲明。另外,method3()本身并不會拋出exception,可是它卻聲明會拋出CheckedException。在向你解釋之前,讓我們先來看看這個類的main()方法:


          public static void main( String[] args )
          {
           ExceptionalClass example = new ExceptionalClass();
           try
           {
            example.method1();
            example.method3();
           }
           catch( CheckedException ex ) { } example.method2( null );
          }




          在main()方法中,如果要調(diào)用method1(),你必須把這個調(diào)用放在try/catch程序塊當(dāng)中,因為它會拋出Checked exception。

          相比之下,當(dāng)你調(diào)用method2()時,則不需要把它放在try/catch程序塊當(dāng)中,因為它會拋出的exception不是checked exception,而是runtime exception。會拋出runtime exception的方法在定義時不必聲明它會拋出exception。

          現(xiàn)在,讓我們再來看看method3()。它調(diào)用了method1()卻沒有把這個調(diào)用放在try/catch程序塊當(dāng)中。它是通過聲明它會拋出method1()會拋出的exception來避免這樣做的。它沒有捕獲這個exception,而是把它傳遞下去。實際上main()方法也可以這樣做,通過聲明它會拋出Checked exception來避免使用try/catch程序塊(當(dāng)然我們反對這種做法)。

          小結(jié)一下:

          * Runtime exceptions:

          在定義方法時不需要聲明會拋出runtime exception;

          在調(diào)用這個方法時不需要捕獲這個runtime exception;

          runtime exception是從java.lang.RuntimeException或java.lang.Error類衍生出來的。

          * Checked exceptions:

          定義方法時必須聲明所有可能會拋出的checked exception;

          在調(diào)用這個方法時,必須捕獲它的checked exception,不然就得把它的exception傳遞下去;

          checked exception是從java.lang.Exception類衍生出來的 

                 
                  Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
                  while (keys.hasMoreElements())
                  {
                      Object key = keys.nextElement();
                      System.out.println(key);
                  }

          主站蜘蛛池模板: 天气| 天水市| 寿阳县| 彰武县| 凯里市| 延长县| 行唐县| 长白| 久治县| 新源县| 尼玛县| 喀什市| 兴文县| 宿松县| 新巴尔虎右旗| 九龙坡区| 五常市| 聂拉木县| 平邑县| 高清| 崇阳县| 读书| 砚山县| 成安县| 信宜市| 井陉县| 政和县| 济源市| 扎囊县| 玉田县| 南和县| 泗洪县| 长兴县| 芜湖市| 南靖县| 荣成市| 上杭县| 顺义区| 乐至县| 泰和县| 富蕴县|