Java中的finally块是什么?
finally块位于try块或catch块之后。无论是否普遍存在Exception,最终的代码块都会永远执行。
示例
public class ExcepTest { public static void main(String args[]) { int a[] = new int[2]; try { System.out.println("访问元素三:" + a[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("抛出异常:" + e); } finally { a[0] = 6; System.out.println("First element value: " + a[0]); System.out.println("The finally statement is executed"); } } }
输出结果
抛出异常: java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executed