您的位置:首页 > 编程语言 > Java开发

我的java学习日记(11)

2014-04-12 23:07 399 查看
Java学习第十一节之异常

一、           异常类型



1、java.lang.NullPointerException(空指针错误)

       String s=null;

             System.out.println("hello");

             s.toString(); 
//仅会打印hello

             System.out.println("java");

2、java.lang.NumberFormatException(数字格式化错误)

Integer.parseInt("123"); //只有int型数据才不会有异常

             Integer.parseInt("s");

             Integer.parseInt(null);

3、java.lang.ArrayIndexOutOfBoundsException
(数组越界异常)   

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

             n[3]=100;

4、java.lang.ClassCastException
(类型转换错误)

             Strings=new String("sss");

             objectobj=s;

             Testt=(Test)obj;

5、java.lang.NoClassDefFoundError
(找不到类,多类的依赖性)

java.lang.NotFoundException

6、java.lang.NoSuchNethodError
(版本差别,1.4以前的版本会报错,但是1.4以后的不会)

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

8、java.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、自定义异常

可继承于Throwable、Exception和RuntimeException等。示例如下:

//操作类

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

      }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: