我的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));

      }

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