package day16.java;
import java.util.Date;
import java.util.Scanner;
import org.junit.Test;
/**
* @author gaoxu
* @user ExceptionTest.java
* @date 2021年3月16日下午4:05:29
* @description 常见的运行时异常
* @return
*/
public class ExceptionTest {
//空指针异常:NullPointerException
@Test
public void NullTest() {
int[] a = null;
System.out.println(a[1]);
String str = null;
System.out.println(str.charAt(0));
}
//下表越界异常:ArrayIndexOutOfBoundsException
@Test
public void OutTest() {
int[] a = new int[3];
System.out.println(a[3]);
}
//类的转换异常:ClassCastException
@Test
public void CastTest() {
Object ob = new Date();
String str = (String) ob;
}
//数据的格式异常:NumberFormatException
@Test
public void FormatTest() {
String str = "123qw";
Integer in = Integer.parseInt(str);
}
//读入数据与定义的接收类型不匹配:InputMismatchException
@Test
public void InputTest() {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(num);
}
//算数类型的异常:ArithmeticException
@Test
public void MathTest() {
int a = 0;
int b = 12;
System.out.println(b / a);
}
}