在 Java 7 之前,即使存在代码冗余,我们也必须为不同类型的异常编写多个异常处理代码。
让我们举个例子。
示例 1:多个 catch 块
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());
}
}
}
输出
/ by zero
在此示例中,可能会发生两个异常
ArithmeticException
,因为我们试图将数字除以 0。ArrayIndexOutOfBoundsException
,因为我们声明了一个新的整数数组,数组边界为 0 到 9,而我们试图为索引 10 赋值。
我们在两个 catch
块中都打印了异常消息,即重复的代码。
赋值运算符=
的结合性是右到左的,因此首先抛出 ArithmeticException
,消息为/ by zero。
在 catch 块中处理多个异常
在 Java SE 7 及更高版本中,我们现在可以在单个 catch
块中捕获多种类型的异常。
可以通过 catch
块处理的每种异常类型都使用竖线或管道符 |
分隔。
其语法为
try {
// code
} catch (ExceptionType1 | Exceptiontype2 ex) {
// catch block
}
示例 2:多 catch 块
class Main {
public static void main(String[] args) {
try {
int array[] = new int[10];
array[10] = 30 / 0;
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
}
}
输出
/ by zero
在单个 catch
块中捕获多个异常可以减少代码重复并提高效率。
编译此程序时生成的字节码将小于具有多个 catch
块的程序,因为没有代码冗余。
注意: 如果 catch
块处理多个异常,则 catch 参数默认为 final
。这意味着我们无法为 catch 参数分配任何值。
捕获基类异常
当在单个 catch
块中捕获多个异常时,规则是从泛化到特化。
这意味着,如果 catch
块中存在异常层次结构,我们可以只捕获基类异常,而不是捕获多个特化异常。
让我们举个例子。
示例 3:仅捕获基类异常
class Main {
public static void main(String[] args) {
try {
int array[] = new int[10];
array[10] = 30 / 0;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
输出
/ by zero
我们知道所有异常类都是 Exception
类的子类。因此,我们可以直接捕获 Exception
类,而不是捕获多个特化异常。
如果基类异常已在 catch
块中指定,请不要在同一 catch
块中使用子类异常。否则,您将收到编译错误。
让我们举个例子。
示例 4:捕获基类和子类异常
class Main {
public static void main(String[] args) {
try {
int array[] = new int[10];
array[10] = 30 / 0;
} catch (Exception | ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
}
}
输出
Main.java:6: error: Alternatives in a multi-catch statement cannot be related by subclassing
在此示例中,ArithmeticException
和 ArrayIndexOutOfBoundsException
都是 Exception
类的子类。因此,我们收到编译错误。
另请阅读