try catch包含多个异常时会怎么执行

try{}catch在Java编程中经常出现,不过你想过下面的这个情况吗?

package com.qjq.mianshi;


public class TryCatch {
public static void main(String[] args) {
int a[] = new int[10];
try {
a[10] = 1;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数据越界");
} catch (Exception e) {
System.out.println("运行异常");
} finally {
System.out.println("Finally");
}
}
}

在这一段代码中输出应该是:

数组越界

Finally

在有多个catch时,只会执行一个范围小的,而不是每一个catch都会执行。

你可能感兴趣的:(Java)