其他复习见https://github.com/pique2233/C-/blob/main/在%20Java%20中.pdf
题目 1:
1 2 3 4 5 6
| public class ArrayTest { public static void main(String[] args) { int[] arr = {1, 2, 3}; System.out.println(arr[5]); } }
|
结果—错误
原因
ArrayIndexOutOfBoundsException 是非受检异常,表示数组越界。由于它是 RuntimeException 的子类,编译器不会强制要求捕获或声明该异常。此异常发生时,程序会终止。
修复方案:
可以使用 try-catch 捕获异常,避免程序终止
1 2 3 4 5 6 7 8 9 10
| public class ArrayTest { public static void main(String[] args) { try { int[] arr = {1, 2, 3}; System.out.println(arr[5]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("数组索引越界异常:" + e.getMessage()); } } }
|
题目 2:
1 2 3 4 5 6 7 8 9 10
| public class DivisionTest { public static void main(String[] args) { divide(10, 0); }
public static void divide(int a, int b) { int result = a / b; System.out.println("结果:" + result); } }
|
结果—错误
原因
ArithmeticException 是非受检异常,表示算术运算错误(例如除以零)。在这种情况下,代码没有显式捕获或声明此异常,程序会在运行时抛出异常并终止。
修复方案:
捕获 ArithmeticException 异常,避免程序崩溃
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class DivisionTest { public static void main(String[] args) { try { divide(10, 0); } catch (ArithmeticException e) { System.out.println("算术异常:" + e.getMessage()); } }
public static void divide(int a, int b) { int result = a / b; System.out.println("结果:" + result); } }
|
题目 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class FileTest { public static void main(String[] args) { readFile("file.txt"); }
public static void readFile(String filename) { try { FileInputStream fis = new FileInputStream(filename); fis.read(); } catch (FileNotFoundException e) { System.out.println("文件未找到:" + e.getMessage()); } catch (IOException e) { System.out.println("读取文件时发生错误:" + e.getMessage()); } } }
|
结果—正确
原因
FileNotFoundException 和 IOException 是受检异常,必须显式声明或捕获。此代码已经通过 try-catch 捕获了这两种异常,因此没有问题。
题目 4:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class ExceptionTest { public static void main(String[] args) { try { throw new MyRuntimeException(); } catch (MyRuntimeException e) { System.out.println("Caught MyRuntimeException"); } } }
class MyRuntimeException extends RuntimeException { private static final long serialVersionUID = 1L; }
|
结果—正确
原因
MyRuntimeException 是非受检异常(继承自 RuntimeException),在 try-catch 块中被成功捕获并处理。由于是非受检异常,编译器不会强制要求声明或捕获它。
题目 5:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class ExceptionDeclarationTest { public static void main(String[] args) throws MyCheckedException { method(); }
public static void method() { throw new MyCheckedException(); } }
class MyCheckedException extends Exception { private static final long serialVersionUID = 1L; }
|
结果—错误
原因
MyCheckedException 是受检异常(继承自 Exception),method() 方法抛出了此异常,但是没有在方法签名中声明 throws MyCheckedException,导致编译错误。
修复方案:
在 method() 方法中声明异常
1 2 3
| public static void method() throws MyCheckedException { throw new MyCheckedException(); }
|
或者在 main() 方法中捕获异常
1 2 3 4 5 6 7
| public static void main(String[] args) { try { method(); } catch (MyCheckedException e) { System.out.println("Caught MyCheckedException"); } }
|
题目 6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class ExceptionChainTest { public static void main(String[] args) { try { method1(); } catch (Exception e) { e.printStackTrace(); } }
public static void method1() throws Exception { try { method2(); } catch (NullPointerException e) { throw new Exception("Caught in method1", e); } }
public static void method2() { throw new NullPointerException("Null value"); } }
|
结果—正确
原因
method2() 抛出了 NullPointerException,该异常在 method1() 中被捕获并封装为 Exception 抛出,形成异常链。
最终,main() 方法捕获并打印了整个异常链的栈信息。