求解 求解 求解
Does this code compile?
1. class SubEx extends Exception { }
2.
3. class Parent {
4. void foo(int i) throws SubEx {
5. if (i > 20)
6. throw new Exception();
7. }
8. }
9.
10. class Kid extends Parent {
11. void foo(int i) throws Exception { //error
12. if (i < 20)
13. throw new SubEx();
14. }
15. }
为什么错,请讲讲
问题点数:20、回复次数:1Top
1 楼ChDw(米)回复于 2001-11-15 10:45:29 得分 20
6. throw new Exception();//这个肯定是错的。因为Parent.foo声明的是SubEx
11. void foo(int i) throws Exception { // 也是错的,因为子类声明的抛出异常只能是父类的子集或者子类
Top





