【发布时间】:2021-09-09 19:45:45
【问题描述】:
我试图捕获多个异常,但我不能。我只能捕获一个异常。
class Main {
public static void main(String[] args) {
try {
int array[] = new int[10];
array[10] = 30 / 0;
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
}
}
【问题讨论】:
-
docs.oracle.com/javase/7/docs/technotes/guides/language/… 这已经存在一段时间了;搜索网络也可以。
-
什么多重例外?当发生被零除错误时,
try块中的代码将停止执行,因此它永远不会到达会导致索引越界错误的代码。 -
一次只能有一个异常,不超过这个,因为当异常发生时它会移动到catch块