Java之常见异常Exception

异常是程序编译或运行时可能会发生的一些小的错误,如果发生会导致程序正常的运行。

编译期异常是可检查的,编译的时候提醒编程的人要处理,运行期异常是不可检查的

 1.编译异常

/**
* function: 1.2.1编译异常 author:wangpeng time:2018年8月13日下午7:45:17
*/
@Test
public void test() {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Date date = sdf.parse("2018-11-11 11:11:11");
}


  2.运行时异常RuntimeException
            2.1NullpoiontException  空指针异常,list为null导致

/**
* function: 1.2.2运行时异常-NullpoiontException空指针异常 author:wangpeng
* time:2018年8月13日下午7:45:44
*/
@Test
public void test1() {
	List<String> list = null;
	list.add("123");
}


            2.2ArrayIndexOutOfBoundsException 数组下标越界异常

/**
* function: 1.2.2运行时异常 ArrayIndexOutOfBoundsException数组下标越界异常 author:wangpeng
* time:2018年8月13日下午7:46:23
*/
@Test
public void test2() {
	String[] str = { "123", "456" };
	System.out.println(str[3]);//数组str下标最大为1
}


            2.3ArithmeticException 除数为0时

/**
* function: 1.2.2运行时异常 ArithmeticException 除数为0时 author:wangpeng
* time:2018年8月13日下午7:47:40
*/
@Test
public void test3() {
	System.out.println(10 / 0);
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值