前言
- 在"Java"中完成异常的处理,快速上手;
实操
【前提】
- 使用"Windows 11"系统通过"IntelliJ IDEA"软件完成;
【实操1】
A
【代码】
Scanner sc = new Scanner(System.in); System.out.println("请输入字母:"); /* 输入"a",空指针异常; 输入"b",数组越界异常; */ String s = sc.nextLine().trim(); if ("a".equalsIgnoreCase(s)) { s = null; s.getBytes(StandardCharsets.UTF_8); System.out.println("这是输入'a'的结果。"); } else if ("b".equalsIgnoreCase(s)) { s = s.split("")[1]; System.out.println("这是输入'b'的结果。"); } System.out.println("触地了......");
【结果】
请输入字母: a Exception in thread "main" java.lang.NullPointerException at ExceptionDemoA.main(ExceptionDemoA.java:15) Process finished with exit code 1
请输入字母: b Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ExceptionDemoA.main(ExceptionDemoA.java:18) Process finished with exit code 1
B
【代码】
Scanner sc = new Scanner(System.in); System.out.println("请输入字母:"); /* 输入"a",空指针异常; 输入"b",数组越界异常; */ String s = sc.nextLine().trim(); if ("a".equalsIgnoreCase(s)) { try { s = null; s.getBytes(StandardCharsets.UTF_8); System.out.println("这是输入'a'的结果。"); } catch (NullPointerException e) { System.out.println("输入'a'出现异常了.."); } } else if ("b".equalsIgnoreCase(s)) { try { s = s.split("")[1]; System.out.println("这是输入'b'的结果。"); } catch (NullPointerException e) { System.out.println("输入'b'出现异常了.."); } } System.out.println("触地了......");
【结果】
请输入字母: a 输入'a'出现异常了.. 触地了...... Process finished with exit code 0
请输入字母: b Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ExceptionDemoB.main(ExceptionDemoB.java:23) Process finished with exit code 1
C
【代码】
Scanner sc = new Scanner(System.in); System.out.println("请输入字母:"); /* 输入"a",空指针异常; 输入"b",数组越界异常; */ String s = sc.nextLine().trim(); try { if ("a".equalsIgnoreCase(s)) { s = null; s.getBytes(StandardCharsets.UTF_8); System.out.println("这是输入'a'的结果。"); } else if ("b".equalsIgnoreCase(s)) { s = s.split("")[1]; System.out.println("这是输入'b'的结果。"); } } catch (NullPointerException e) { System.out.println("出现'空指针异常'了"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("出现'数组越界异常'了"); } System.out.println("触地了......");
【结果】
请输入字母: a 出现'空指针异常'了 触地了...... Process finished with exit code 0
请输入字母: b 出现'数组越界异常'了 触地了...... Process finished with exit code 0
D
【代码】
Scanner sc = new Scanner(System.in); System.out.println("请输入字母:"); /* 输入"a",空指针异常; 输入"b",数组越界异常; */ String s = sc.nextLine().trim(); try { if ("a".equalsIgnoreCase(s)) { s = null; s.getBytes(StandardCharsets.UTF_8); System.out.println("这是输入'a'的结果。"); } else if ("b".equalsIgnoreCase(s)) { s = s.split("")[1]; System.out.println("这是输入'b'的结果。"); } throw new RuntimeException(); } catch (NullPointerException e) { System.out.println("出现'空指针异常'了"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("出现'数组越界异常'了"); } System.out.println("触地了......");
【结果】
请输入字母: a 出现'空指针异常'了 触地了...... Process finished with exit code 0
请输入字母: c Exception in thread "main" java.lang.RuntimeException at ExceptionDemoD.main(ExceptionDemoD.java:22) Process finished with exit code 1
E
【代码】
Scanner sc = new Scanner(System.in); System.out.println("请输入字母:"); /* 输入"a",空指针异常; 输入"b",数组越界异常; */ String s = sc.nextLine().trim(); try { if ("a".equalsIgnoreCase(s)) { s = null; s.getBytes(StandardCharsets.UTF_8); System.out.println("这是输入'a'的结果。"); } else if ("b".equalsIgnoreCase(s)) { s = s.split("")[1]; System.out.println("这是输入'b'的结果。"); } throw new RuntimeException(); } catch (NullPointerException | ArrayIndexOutOfBoundsException e) { System.out.println("出现'空指针异常'或'数组越界异常'了"); } catch (Exception e) { System.out.println("出现'异常'了.........."); } System.out.println("触地了......");
【结果】
请输入字母: a 出现'空指针异常'或'数组越界异常'了 触地了...... Process finished with exit code 0
请输入字母: b 出现'空指针异常'或'数组越界异常'了 触地了...... Process finished with exit code 0
请输入字母: c 出现'异常'了.......... 触地了...... Process finished with exit code 0
F
【代码】
Scanner sc = new Scanner(System.in); System.out.println("请输入字母:"); /* 输入"a",空指针异常; 输入"b",数组越界异常; */ String s = sc.nextLine().trim(); try { if ("a".equalsIgnoreCase(s)) { s = null; s.getBytes(StandardCharsets.UTF_8); System.out.println("这是输入'a'的结果。"); } else if ("b".equalsIgnoreCase(s)) { s = s.split("")[1]; System.out.println("这是输入'b'的结果。"); } throw new RuntimeException(); } catch (Exception e) { System.out.println("出现'异常'了.........."); } catch (NullPointerException | ArrayIndexOutOfBoundsException e) { System.out.println("出现'空指针异常'或'数组越界异常'了"); } System.out.println("触地了......");
【结果】
运行不了......
后记
- 和此文相关的所有内容,需要的请下载;