例外として、デフォルトではクラスがあり、例外クラスはThrowable
です。なぜexceptionはクラスであり、javaのインタフェースではないのですか?
私の質問は、なぜ例外はインターフェイスではないのですか?
インターフェイスとして例外クラスを作成しようとすると、エラーが表示されます。私はそれを試してみましたが、私はまだ同じ理由を見つけることができません。 これは私が試したコードは次のとおりです。
インタフェース:
package com.prg;
import com.thed.util.CustomException ;
public interface ExceptionInterface extends Throwable{
public String sayHello() throws CustomException {
return null;
}
}
クラス:
package com.prg;
import com.thed.util.CustomException ;
public class ExceptionCheck implements ExceptionInterface {
public String sayHello() {
return "Hey";
}
public ExceptionCheck() throws CustomException {
System.out.println("hi");
}
public static void main (String[] args) throws CustomException {
System.out.println("how are you");
throw new ExceptionInterface({
public String sayHello() { return "Hey"; }
});
}
}
私はそれが期待されている例外を示していないクラスへのインターフェイスを変更していた場合。しかし、なぜこれは私の質問ですか?
それは彼らが設計した方法なので。 – EJP