选出输出结果正确的选项()

public class Test {
    public static String outPut = "";

    public static void foo(int i) {
        try {
            if (i == 1)
                throw new Exception();
            outPut += "1";
        } catch (Exception e) {
            outPut += "2";
            return;
        } finally {
            outPut += "3";
        }
        outPut += "4";
    }

    public static void main(String[] args) {
        foo(0);
        System.out.println(outPut);
        foo(1);
        System.out.println(outPut);
    }
}
A: 134    13423  
B: 1      134  
C: 134    34  
D: 134    13413

参考解答

A

分析

执行foo(0)时,没有出现异常,执行try, finally, finally 之后的代码,结果为: 134

执行foo(1)时,出现了异常,执行catch, finally 的代码,由于catch中有return语句,所以finally之后的代码不会执行。 结果为:23,是两次方法调用的结果拼接给同一个字符串变量,因此最终结果为:13423


results matching ""

    No results matching ""