常见的异常有以下:
java.lang.RuntimeException
ClassCastException
ArrayIndexOutOfBoundsException
NullPointerException
ArithmeticException
NumberFormatException
InputMismatchException
java.io.IOExeption
FileNotFoundException
EOFException
java.lang.ClassNotFoundException
java.lang.InterruptedException
java.io.FileNotFoundException
java.sql.SQLException
运行时异常:
1、ArrayIndexOutOfBoundsException
public static void main(String[] args) {
int[] arr = new int[5];
for(int i = 0 ; i < 10;i++) {
System.out.println(arr[i]);
}
}
2、NullPointerException
public static void main(String[] args) {
int[] arr = new int[5];
arr = null;
for(int i = 0 ; i < 10;i++) {
System.out.println(arr[i]);
}
}
3、ArithmeticException
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a/ b);
}
4、ClassCastException
public static void main(String[] args) {
Object obj = new SalariedEmployee();
HourlyEmployee he = (HourlyEmployee)obj;
}
5、InputMismatchException
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
}
编译期异常:
1、ParseException
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String strDate = sdf.format(date);
System.out.println(strDate);
String dateStr = "2019-11-11";
Date d =null;
try {
d = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(d);
}