Java异常和后置条件

时间:2014-08-14 23:02:00

标签: java class exception return computer-science

会发生以下哪种异常,会发生什么异常?

  

//前置条件:a不为空

public String sampleClass(ArrayList<String> a, int b) {
    String t;
    int u;
    for (u = 0; u <= a.size(); u++) {
        t = (String)(a.get(u));
        if (t.length() == b) {
            return t;
        }
    }
    return null;
}
  • A)NullPointerException
  • B)IndexOutOfBoundsException
  • C)ClassCastException

我选择的答案:我用测试员运行程序,我得到了B.我认为B就是答案。

2 个答案:

答案 0 :(得分:1)

如果我们不知道如何调用某事,那么你的问题就没有意义了。根据情况你几乎可以得到这些例外:

在某些情况下,您不会得到任何例外。例如:

ArrayList<String> a = new ArrayList<>();
a.add("");
doSomething(a,0);

以下内容将抛出NullPointerException:

ArrayList<String> a = new ArrayList<>();
a.add(null);
doSomething(a,0);

这个会抛出一个IndexOutOfBoundException:

doSomething(new ArrayList<>(),0)

最后,你会遇到一个ClassCastException:

ArrayList a = new ArrayList();
a.add(new Object());
doSomething(a,0);

D和E都不可能(D只有在您使用数组并且您的代码不包含任何可能引发异常的特定算术运算时才会发生。)

答案 1 :(得分:0)

下面是一个完全可运行的示例,它显示了A,B,C并且没有发生异常。

如果没有说明ArrayList包含的内容,则无法完全回答这个问题。

public class Test {
    public static void main(final String... args) {
        try {
            Test.throwNullPointer();
        } catch (final Throwable e) {
            System.out.println(e);
        }

        try {
            Test.throwIndexOutOfBoundsException();
        } catch (final Throwable e) {
            System.out.println(e);
        }

        try {
            Test.throwClassCastException();
        } catch (final Throwable e) {
            System.out.println(e);
        }

        try {
            Test.noException();
            System.out.println("No Exception");
        } catch (final Throwable e) {
            System.out.println(e);
        }
    }

    public static void throwNullPointer() {
        final ArrayList<String> a = new ArrayList<>();
        a.add("a");
        a.add(null);
        a.add("c");
        Test.doSomething(a, 0);
    }

    public static void throwIndexOutOfBoundsException() {
        final ArrayList<String> a = new ArrayList<>();
        a.add("a");
        a.add("b");
        a.add("c");
        Test.doSomething(a, 0);
    }

    public static void throwClassCastException() {
        final ArrayList<String> a = new ArrayList<>();
        Test.doNaughtyStuff(a);
        Test.doSomething(a, 0);
    }

    private static void doNaughtyStuff(final ArrayList a) {
        a.add(new Object());
    }

    public static void noException() {
        final ArrayList<String> a = new ArrayList<>();
        a.add("");
        Test.doSomething(a, 0);
    }

    public static String doSomething(final ArrayList<String> a, final int b) {
        String t;
        int u;
        for (u = 0; u <= a.size(); u++) {
            t = a.get(u);
            if (t.length() == b) {
                return t;
            }
        }
        return null;
    }
}

输出:

java.lang.NullPointerException
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
No Exception
相关问题