1 package day4.haifei03;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 /*
8 3.5 编译时异常和运行时异常的区别
9 Java中的异常分为两类
10 编译时异常(受检异常)
11 都是Exception类及其子类
12 必须显示处理,否则程序就会发生错误,无法通过编译
13 运行时异常(非受检异常)
14 都是RuntimeException类及其子类
15 无需显示处理,也可以和编译时异常一样处理
16 */
17 public class ExceptionDemo3 {
18
19 public static void main(String[] args) {
20 method();
21 }
22
23 /*public static void show(){
24 String s = "2021-05-19";
25 SimpleDateFormat sdf = new SimpleDateFormat();
26 Date d = sdf.parse(s); //编译时异常,红色波浪线提醒
27 System.out.println(d);
28 }*/
29
30 public static void show(){
31 try {
32 String s = "2021-05-19";
33 SimpleDateFormat sdf = new SimpleDateFormat();
34 Date d = sdf.parse(s);
35 System.out.println(d);
36 }catch (ParseException e){
37 e.printStackTrace();
38 }
39 }
40
41 /*public static void method(){
42 int[] arr = {1, 2, 3};
43 System.out.println(arr[4]); //编译未报错,属于运行时异常
44 }*/
45
46 public static void method(){
47 try {
48 int[] arr = {1, 2, 3};
49 System.out.println(arr[4]);
50 }catch (ArrayIndexOutOfBoundsException e){
51 e.printStackTrace();
52 }
53 }
54
55 }