|
@@ -0,0 +1,108 @@
|
|
|
|
|
+package exercise.exercise07;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author WanJl
|
|
|
|
|
+ * @version 1.0
|
|
|
|
|
+ * @title ExceptionMethodDemo
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * @create 2026/7/28
|
|
|
|
|
+ */
|
|
|
|
|
+public class ExceptionMethodDemo {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 任务 1:体验 getMessage() 和 printStackTrace()
|
|
|
|
|
+ * 传入一个数组和索引,尝试访问该索引的元素
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void accessArray(int[] arr, int index) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ System.out.println("数组元素:" + arr[index]);
|
|
|
|
|
+ } catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
|
+ // 1. 输出异常的描述信息(getMessage())
|
|
|
|
|
+ System.out.println("getMessage() 输出:" + e.getMessage());
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 输出异常的完整堆栈信息(printStackTrace())
|
|
|
|
|
+ System.out.println("printStackTrace() 输出(请补充调用):");
|
|
|
|
|
+ // 请在下面调用 e.printStackTrace();
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ // 3. 输出异常的类名
|
|
|
|
|
+ System.out.println("异常类名:" + e.getClass().getName());
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 输出异常的原因(getCause())
|
|
|
|
|
+ System.out.println("getCause() 输出:" + e.getCause());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 任务 2:体验 fillInStackTrace()
|
|
|
|
|
+ * 模拟多层方法调用中的异常传递
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void methodA() throws Exception {
|
|
|
|
|
+ methodB(); // 调用 methodB,methodB 会抛出异常
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void methodB() throws Exception {
|
|
|
|
|
+ // 请在此处手动抛出一个 Exception,描述信息为 "methodB 中发生异常"
|
|
|
|
|
+ // 使用 throw new Exception("...")
|
|
|
|
|
+ throw new Exception("methodB 中发生异常");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void testFillInStackTrace() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ methodA();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("原始异常信息:" + e.getMessage());
|
|
|
|
|
+ System.out.println("原始堆栈(调用 printStackTrace):");
|
|
|
|
|
+ e.printStackTrace(); // 显示原始异常发生位置
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("\n--- 以下为 fillInStackTrace 后的异常 ---");
|
|
|
|
|
+ // 调用 fillInStackTrace() 会重置堆栈信息到当前行
|
|
|
|
|
+ // 使得异常看起来像是在当前方法抛出的
|
|
|
|
|
+ //Throwable newException = e.fillInStackTrace();
|
|
|
|
|
+ //newException.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 任务 3:模拟异常链(getCause)
|
|
|
|
|
+ * 将底层异常包装为高层异常,保留 cause 信息
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void readFile(String path) throws Exception {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 模拟一个底层异常:文件未找到
|
|
|
|
|
+ throw new FileNotFoundException("文件 " + path + " 不存在!");
|
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
|
+ // 将底层异常包装为高层异常,并保留 cause
|
|
|
|
|
+ throw new Exception("读取文件失败,路径:" + path, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void testExceptionChain() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ readFile("/data/config.txt");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("===== 异常链测试 =====");
|
|
|
|
|
+ System.out.println("高层异常信息:" + e.getMessage());
|
|
|
|
|
+ System.out.println("高层异常类名:" + e.getClass().getName());
|
|
|
|
|
+
|
|
|
|
|
+ // 获取底层异常(cause)
|
|
|
|
|
+ Throwable cause = e.getCause();
|
|
|
|
|
+ System.out.println("\n底层异常(cause):" + cause);
|
|
|
|
|
+ System.out.println("底层异常信息:" + cause.getMessage());
|
|
|
|
|
+ System.out.println("底层异常类名:" + cause.getClass().getName());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ System.out.println("========== 任务 1:getMessage 与 printStackTrace ==========");
|
|
|
|
|
+ int[] arr = {1, 2, 3};
|
|
|
|
|
+ accessArray(arr, 5);
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("\n========== 任务 2:fillInStackTrace ==========");
|
|
|
|
|
+ testFillInStackTrace();
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("\n========== 任务 3:异常链 getCause ==========");
|
|
|
|
|
+ testExceptionChain();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|