【发布时间】:2015-02-15 21:13:08
【问题描述】:
Java String 类的 CharAt 方法抛出 StringIndexOutOfBoundsException。但是 Java API 文档说它会抛出一个IndexOutOfBoundsException。我知道StringIndexOutOfBoundsException 是IndexOutOfBoundsException 的子类。但是捕获StringIndexOutOfBoundsException 而不是IndexOutOfBoundsException 是否不正确?
这里是charAt方法的代码
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
【问题讨论】:
-
是的,因为 IndexOutOfBoundsException 由于继承可能是 StringIndexOutOfBoundsException