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

猜你喜欢

转载自blog.csdn.net/qq_34361514/article/details/81697597