练习程序
package _4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//编译时异常和运行时异常的区别
public class _217 {
public static void main(String[] args) {
mthod1();
mthod2();
}
//编译时异常
public static void mthod1() {
try {
String s = "2022-3-20";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println();
}catch (ParseException e){
e.printStackTrace();
}
}
//运行时异常
public static void mthod2() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);//索引越界ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}