【发布时间】:2014-12-19 11:52:40
【问题描述】:
考虑如下界面:
package hf;
public interface BadInterface
{
void meth() throws Exception;
}
由以下类实现:
package hf;
public class apples implements BadInterface
{
public static void main(String[] args)
{
new apples().meth();
}
public void meth()
{
System.out.println("Ding dong meth.");
}
}
虽然 meth() 是一个抛出异常的方法,但方法 meth() 的调用者不必处理或声明异常,程序就可以成功运行。为什么会这样? 是不是违反了每当你调用一个抛出异常的方法时,都需要捕获异常或者声明自己抛出异常的规则?
【问题讨论】:
-
您应该将您的类重命名为
Apples以符合 Java 编码规范。
标签: java exception interface exception-handling try-catch