【问题标题】:IndexOutOfBounds Exception Java String Class charAt MethodIndexOutOfBounds Exception Java String 类 charAt 方法
【发布时间】:2015-02-15 21:13:08
【问题描述】:

Java String 类的 CharAt 方法抛出 StringIndexOutOfBoundsException。但是 Java API 文档说它会抛出一个IndexOutOfBoundsException。我知道StringIndexOutOfBoundsExceptionIndexOutOfBoundsException 的子类。但是捕获StringIndexOutOfBoundsException 而不是IndexOutOfBoundsException 是否不正确?

这里是charAt方法的代码

public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }

【问题讨论】:

  • 是的,因为 IndexOutOfBoundsException 由于继承可能是 StringIndexOutOfBoundsException

标签: java string charat


【解决方案1】:

这不是错误的,因为这是该方法实际抛出的内容,并且因为它是 RuntimeException(即未选中),所以没关系,因为您根本不必捕获它。

现在,如果它是一个已检查的异常:

public char someMethod (int index) throws SomeCheckedException {
    if (index < 0) {
        throw new SomeSubCheckedException (index); // subclass of SomeCheckedException 
    }
    return something;
}

这里,如果你在 try 块中调用 someMethod 并且只捕获 SomeSubCheckedException,则代码不会通过编译,因为就编译器而言,someMethod 可能会抛出 @987654326 的实例@ 不是SomeSubCheckedException

【讨论】:

    【解决方案2】:

    不,这不是错误的。但是您可以节省一些击键并只使用 IndexOutOfBoundsException,除非您有一个使用字符串索引的方法,例如数组索引或列表索引等。然后您可以区分异常类型以不同地处理案例。

    【讨论】:

      【解决方案3】:

      它只是最近抛出的异常,有什么需要有这么多内置的异常类,而我们总是可以throw/use Exception class

      ArrayIndexOutOfBoundsException 用于数组,StringIndexOutOfBoundsException 用于处理字符串等。

      更多here

      【讨论】:

        【解决方案4】:

        在这种情况下,我更喜欢IndexOutOfBoundsException

         public static void main(String[] args) {
            String s = "abc";
            String[] arr = new String[1];
            for (int i = 0; i < 2; i++) {
                try {
                    s.charAt(5);
                    System.out.println(arr[2]);
                } catch (IndexOutOfBoundsException e) { // catch both ArrayIndexOutOfBounds as well as StringIndexOutOfBounds and treat them similarly.
                    System.out.println("caught");
                    s = "aaaaaaaaaa";
                    e.printStackTrace();
        
                }
            }
        
        }
        
        O/P :
        0
        java.lang.StringIndexOutOfBoundsException: String index out of range: 5
        caught
        caught
            at java.lang.String.charAt(Unknown Source)
            at StaticAssign.main(Sample.java:41)
        java.lang.ArrayIndexOutOfBoundsException: 2
            at StaticAssign.main(Sample.java:42)
        

        没有错。这只是一个设计考虑。这同样适用于IOExceptionFileNotFoundException

        【讨论】:

          【解决方案5】:

          您不应该查看特定的实现。文档说它会抛出IndexOutOfBoundsException,然后如果你想处理这个(这不是真的必要)你最好抓住它。

          可能有不同的 Java 实现不进行检查,而只是让数组访问抛出 ArrayIndexOutOfBoundsException,或者在下一个版本中,Oracle 可能会决定这样做。

          仅处理StringIndexOutOfBoundsException 会将您耦合到特定的实现,而不是API 文档中描述的一般合同。

          我上面的例子纯属假设,因为StringIndexOutOfBoundsException 的文档明确表明String 应该抛出它,但作为一般规则:遵守合同。

          【讨论】:

            猜你喜欢
            • 2017-02-26
            • 2021-09-03
            • 1970-01-01
            • 2021-02-01
            • 2015-06-07
            • 2014-09-16
            • 1970-01-01
            • 2018-11-26
            • 1970-01-01
            相关资源
            最近更新 更多