最终可以在Java中不受阻碍地使用块吗?
是的,最终使用catch块不是强制性的。您可能必须尝试最后。
示例
public class Test {
public static void main(String args[]) {
int a[] = new int[2];
try {
} finally {
a[0] = 6;
System.out.println("First element value: " + a[0]);
System.out.println("The finally statement is executed");
}
}
}输出结果
First element value: 6
Exception in thread "main" The finally statement is executed
java.lang.ArrayIndexOutOfBoundsException: 3
at a5Exception_Handling.Test.main(Test.java:7)