Java 调用超类构造函数和方法时的异常处理

Java 调用超类构造函数和方法时的异常处理,java,Java,例如,我有一个父类,其构造如下: public class ArrayIndexedList<T> implements IndexedList<T> { private final T[] data; public ArrayIndexedList(int size, T defaultValue) throws LengthException { if (size <= 0) { throw new LengthException

例如,我有一个父类,其构造如下:

public class ArrayIndexedList<T> implements IndexedList<T> {

  private final T[] data;

  public ArrayIndexedList(int size, T defaultValue) throws LengthException {
    if (size <= 0) {
      throw new LengthException();
    }
    if (defaultValue == null) {
      return;
    }
    for (int i = 0; i < size; i++) {
      data[i] = defaultValue;
    }
  }

}
公共类ArrayIndexedList实现IndexedList{
私有最终T[]数据;
public ArrayIndexedList(int size,T defaultValue)抛出长度异常{

如果(size您不能。如果它是一个选中的异常,至少不能。(属于
异常
的子类的任何异常,但不是
运行时异常
),那么您唯一的选择就是使用
抛出异常
向上传递异常

如果是未经检查的异常(属于
RuntimeException
子类的任何异常),则无需执行任何操作


一个很好的例子是ArrayList,它的
ArrayList(int-capacity)
构造函数。如果容量小于0,它将抛出一个
IllegalArgumentException
。这是一个未经检查的异常,因此由调用方决定是否要捕获它。

我想你不能。为什么不将
抛出长度异常添加到子类构造函数中?
public class MeasuredIndexedList<T> extends ArrayIndexedList<T> {

  private int accessCount;
  private int mutationCount;

  public MeasuredIndexedList(int size, T defaultValue) {
    super(size, defaultValue); //how to take the exception thrown by this line? 
    accessCount = 0;
    mutationCount = 0;
  }

}