as per my understanding it is a checked exception.
and.. as per rules of overriding..
the overridding method should not throw new or broader exceptions.
hence i am confused with the below question in k&B book excercise for chapter -5 qustion no.3..
only a & d should be correct.
but answer involves e&f also which throw new exception numberformatexception. thats my confusion.
------------
class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
class Dog2 extends Animal {
public void eat() { // no Exceptions }
public static void main(
String [] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat(); // ok
a.eat(); // compiler error -
// unreported exception
}
}
------------
Which, inserted independently at // insert code here, will compile, and produce the output
b? (Choose all that apply.)
A. String doFileStuff() { return "b"; }
B. String doFileStuff() throws IOException { return "b"; }
C. String doFileStuff(int x) throws IOException { return "b"; }
D. String doFileStuff() throws FileNotFoundException { return "b"; }
E. String doFileStuff() throws NumberFormatException { return "b"; }
F. String doFileStuff() throws NumberFormatException,
FileNotFoundException { return "b"; }
Answer:
�� 3 A, D, E, and F are correct. It���s okay for an overriding method to throw the same
exceptions, narrower exceptions, or no exceptions. And it���s okay for the overriding
method to throw any runtime exceptions.
���� B is incorrect, because the overriding method is trying to throw a broader exception.
C is incorrect. This method doesn���t override, so the output is a. (Objective 2.4)