Java 子类不能有父类异常

Java 子类不能有父类异常,java,exception,overriding,Java,Exception,Overriding,我读到子类方法不能有父类方法的异常。在异常层次结构中,算术异常是从RuntimeException派生的,对吗?所以下面的代码应该给出编译错误。但这不是给予,有人能告诉我为什么吗 class Cafe { void f() throws ArithmeticException{ throw new ArithmeticException(); } } class Coffee extends Cafe { void f() throws RuntimeEx

我读到子类方法不能有父类方法的异常。在异常层次结构中,算术异常是从RuntimeException派生的,对吗?所以下面的代码应该给出编译错误。但这不是给予,有人能告诉我为什么吗

class Cafe
{
    void f() throws ArithmeticException{
        throw new ArithmeticException();
    }
}
class Coffee extends Cafe
{
    void f() throws RuntimeException{
        System.out.println("hi");
    }

    public static void main(String[] args)throws ArithmeticException{
        Cafe c=new Cafe();
        c.f();

    }
}

方法重写规则仅适用于检查的异常,而非
运行时
异常。原因是您可以随时抛出任何
运行时异常
,编译器不会强制您处理它或在方法签名中声明throws子句。

因为
RuntimeException
及其子类是未检查的异常。没有必要在条款中提及它们