原文: https://www.programiz.com/java-programming/multiple-exceptions
在本教程中,我们将借助示例学习如何在 Java 中处理多个异常。
在 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与消息 /零一起抛出。
在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块中捕获多个异常时,该规则将被通用化为Exception。
这意味着,如果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类的子类。 因此,我们得到一个编译错误。
