众所周知,异常是默认类,异常类扩展为Throwable
。为什么异常是一个类而不是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