考虑以下Java代码片段: ```java try { int[] arr = new int[2]; arr[3] = 5; //可能抛出ArrayIndexOutOfBoundsException } catch (Exception e) { if (e instanceof RuntimeException) { System.out.println("Runtime Exception"); } else { System.out.println("Other Exception"); } } finally{ System.out.println("Finally block executed"); } ``` 该代码的输出结果是什么?

答案解析

代码中arr[3] = 5; 会导致ArrayIndexOutOfBoundsException异常,该异常是RuntimeException的子类。catch块捕获Exception及其子类异常。if条件判断e是否为RuntimeException实例,因为ArrayIndexOutOfBoundsException是RuntimeException的子类,所以会输出“Runtime Exception”。finally块无论是否发生异常都会执行,所以会输出“Finally block executed”。因此最终输出为“Runtime Exception”,换行,然后输出“Finally block executed”。D选项错误,因为有catch块,所以程序不会直接终止。
正确答案:A
随机推荐
开始刷题