【Java】【异常】【写出常见的异常:运行时异常、受检查时异常(各5个)】2016.02.28...

 一、运行时异常(RuntimeException)
1、
NullPointerException(空指针异常)
【Java】【异常】【写出常见的异常:运行时异常、受检查时异常(各5个)】2016.02.28..._第1张图片

 

package com.geminno.homework0222;

public class ExceptionExample01 {

//    

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        String i=null;

        System.out.println(i.toString());

    }

}

结果:28194740_81tc.jpg
 
 2、ArrayIndexOutOfBoundsException(数组下标越界异常)
28194740_bY3P.jpg

package com.geminno.homework0222;


public class IndexOutOfBoundsException02 {

public static void main(String[] args) {

String []arrays=new String[5];

for (int i = 0; i <= arrays.length; i++) {

arrays[i]=i+1+" ";

}

for (int i = 0; i < arrays.length; i++) {

System.out.println(arrays[i]);

}

}


}

 
结果: 
28194740_6DqG.jpg 
 
3、ClassCastException(类型转换异常)
 28194740_35F7.jpg
 
package com.geminno.homework0222;



public class ClassCastException03 {

public static void main(String[] args) {

Object i="hahaha";

System.out.println((Integer)i);

}


}


结果:28194740_Lxhr.jpg
 
 
4、ArithmeticException(数学异常) 
 28194740_8eXX.jpg

package com.geminno.homework0222;


public class ArithmeticException04 {


public static void main(String[] args) {

int i=5/0;

System.out.println(i);

}


}


结果:

 28194740_2ooV.jpg

 
 5、ArrayStoreException(存储数组类型异常)
 【Java】【异常】【写出常见的异常:运行时异常、受检查时异常(各5个)】2016.02.28..._第2张图片

 
package com.geminno.homework0222;


public class ArrayStoreException05 {

public static void main(String[] args) {

Object []arrays=new String[3];

arrays[0]="警察";

arrays[1]=new Integer(564);

arrays[2]=new Double(4512);

}


}


结果:28194741_NhRe.jpg


5、NegativeArraySizeException(数组长度为负数异常)
 28194741_t18X.jpg
 
package com.geminno.homework0222;


public class IndexOutOfBoundsException02 {

public static void main(String[] args) {

String []arrays=new String[-1];

for (int i = 0; i <= arrays.length; i++) {

arrays[i]=i+1+" ";

}

for (int i = 0; i < arrays.length; i++) {

System.out.println(arrays[i]);

}

}


}


结果:
28194741_bbP8.jpg






一、检查时异常
说明:此类异常在JVM上都不会编译通过,即在javac是就不会通过,如果不对其进行处理,程序就不会编译通过

常见的受检查时异常有:
 
1、ClassNotFoundException

 2、DataFormatException
 3、IOException
 
4、SQLException
 5、 FileNotFoundException 
 6
 ZipException


转载于:https://my.oschina.net/YongfengHe/blog/625515

你可能感兴趣的:(【Java】【异常】【写出常见的异常:运行时异常、受检查时异常(各5个)】2016.02.28...)