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

 一、运行时异常(RuntimeException)
1、
NullPointerException(空指针异常)
28194740_1YV6.jpg

 

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(存储数组类型异常)
 28194741_iiGj.jpg

 
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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值