class Tire {
void doStuff() {}
}
public class Retread extends Tire {
public static void main(String[] args) {
new Retread().doStuff();
}
void doStuff() throws ArithmeticException{
System.out.print(7/0);
}
}
重写了 Tire的doStuff(),但是重写的 doStuff()抛出ArithmeticException
却能通过编译,为什么能通过? 我看书上说重写方法不能抛出比它更广的检验异常, Tire的doStuff()都没有声明异常,按理说重写方法也不能声明异常啊?
void doStuff() {}
}
public class Retread extends Tire {
public static void main(String[] args) {
new Retread().doStuff();
}
void doStuff() throws ArithmeticException{
System.out.print(7/0);
}
}
重写了 Tire的doStuff(),但是重写的 doStuff()抛出ArithmeticException
却能通过编译,为什么能通过? 我看书上说重写方法不能抛出比它更广的检验异常, Tire的doStuff()都没有声明异常,按理说重写方法也不能声明异常啊?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货