i注意到StringBuilder
间接实现Appendable
。
Appendable.append(CharSequence)
抛出了检查的异常IOException
。那么AbstractStringBuilder.append(CharSequence)
如何不宣布IOException
呢?因此,当我附加到StringBuilder
时,我不必担心IOException
?
这只是针对Appendable
改装的现有类的一些奇怪例外,还是我忘记了我的基本Java规则的一部分?
实现接口时,您可能不会引发异常,因此您可以以下内容:
new Appendable() {
@Override
public Appendable append(CharSequence charSequence) {
return null;
}
@Override
public Appendable append(CharSequence charSequence, int i, int i1) {
return null;
}
@Override
public Appendable append(char c) {
return null;
}
}
实现接口或扩展抽象类时,您可以决定是否提出异常。
不管覆盖方法是否引发异常,都可以抛出任何未检查的例外。但是,覆盖方法不应抛出比覆盖方法声明的例外情况。