我的java學習日記(11)

Java學習第十一節之異常

一、           異常類型

1、java.lang.NullPointerException(空指針錯誤)

       String s=null;

             System.out.println("hello");

             s.toString();  //僅會打印hello

             System.out.println("java");

2java.lang.NumberFormatException(數字格式化錯誤)

Integer.parseInt("123"); //只有int型數據纔不會有異常

             Integer.parseInt("s");

             Integer.parseInt(null);

3java.lang.ArrayIndexOutOfBoundsException (數組越界異常)   

int[] n={1,2,3};

             n[3]=100;

4java.lang.ClassCastException (類型轉換錯誤)

             Strings=new String("sss");

             objectobj=s;

             Testt=(Test)obj;

5java.lang.NoClassDefFoundError (找不到類,多類的依賴性)

java.lang.NotFoundException

6java.lang.NoSuchNethodError (版本差別,1.4以前的版本會報錯,但是1.4以後的不會)

7java.lang.ArithmeticException (不能被0整除)

8java.lang.OutOfMemoryError (內存不夠)

二、異常運行

 1、異常代碼

  

 

privateString name;   

      publicvoid setName(String name) throws Exception{

             

             if(name.length()<=0|| name.length()>=4){

                    

                    thrownew Exception();     //拋出異常後,自動終止代碼運行

             }

             this.name=name;

      }

      publicstatic void main(String[] args){    //方法、構造器等均可拋出異常

             try{

                    newtest1().setName("1233");

             }catch(Exceptione){

                    System.out.println("名字長度不符");

             }

      }

2、自定義異常

可繼承於ThrowableExceptionRuntimeException等。示例如下:

//操作類

publicclass test4 {

public void mm()throwsNameLengthException{

       System.out.println("0");

       throw new NameLengthException("1");

}

public void mm1()throwsException{

       System.out.println("2");

       mm();

       try{

              mm();

       }catch(NameLengthException e){

              System.out.println("3");

              throw e;

       }finally{              

              System.out.println("4");                   

       }

}

public void mm2()throwsException{

       System.out.println("5");

       mm1();

       System.out.println("6");

}

public static voidmain(String[] args)throws Exception{

       System.out.println("7");

       new test4().mm2();

       System.out.println("8");

}

}

 

//異常類

publicclass NameLengthException extends RuntimeException{

 

public NameLengthException(String s) {

       super(s);

}

}

3、封裝數據類型

 數據類型   

  int      Integer

  floate   Floate

  double  Double

  char    Character     //1.4版本前區分,之後不以區分

  long     Long

  byte    Byte

  boolean  Boolean

 代碼練習:

publicstaticvoid main(String[] args){

             System.out.println(Integer.parseInt("14"));

             System.out.println(Integer.parseInt("14",16));

             System.out.println(Integer.parseInt("14",8));

             System.out.println(Integer.parseInt("1010101011101",2));

             

             System.out.println(Integer.SIZE);

             System.out.println(Integer.MAX_VALUE);

             System.out.println(Integer.MIN_VALUE);

             

             System.out.println(Integer.toBinaryString(14));

             System.out.println(Integer.toHexString(14));

             System.out.println(Integer.toOctalString(14));

      }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章