首页 > 试题广场 >

以下代码执行输出结果为( ) public class Te

[单选题]
以下代码执行输出结果为(      )
public class Test{
    public static void main(String args[]){
        int a[] = new int[2];
        try{
            System.out.println(a[3]);
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Exception thrown: ArrayIndexOutOfBoundsException");
            return;
        }
        finally{
            System.out.println("The finally statement is executed");
        }

        System.out.println("The test is end");
        return;
    }
}
  • Exception thrown: ArrayIndexOutOfBoundsException
    The finally statement is executed
    The test is end
  • Exception thrown: ArrayIndexOutOfBoundsException
    The test is end
  • Exception thrown: ArrayIndexOutOfBoundsException
    The finally statement is executed
  • Exception thrown: ArrayIndexOutOfBoundsException
这题旨在考查try-catch-finally语句块的使用,很显然,因为ArrayIndexOutOfBoundsException的异常会进入到catch当中,但由于存在finally语句块,所以在catch语句当中的return之前,会先执行finally语句中的方法,而又由于已经再catch语句当中就return了,所以第三步也就输出不了了。
发表于 2021-09-25 22:17:17 回复(0)