About Question enthuware.ocajp.i.v7.2.1093 :
Posted: Sat Jun 15, 2013 7:43 pm
In this program,
the options- i) method1 in class NewBase does not need to specify any exceptions. v)There is no problem with the code ------are correct
But my question is since IOException is a checked exception it needs to be handled. So the option iii) method1 in class NewBase must at least give IOException in its throws clause. ----is correct.
When i ran this program in Eclipse it works fine but when i add main() method- public static void main(String[] args){
Base b = new Base ();
b.method1();
} then it shows error as unhandled type IOException
So do i have to analyse the code as given or analyse it after adding main() method.
Please advise!
Code: Select all
class Base{
void method1() throws java.io.IOException, NullPointerException{
someMethod("arguments");
// some I/O operations
}
int someMethod(String str){
if(str == null) throw new NullPointerException();
else return str.length();
}
}
public class NewBase extends Base{
void method1(){
someMethod("args");
}
}
the options- i) method1 in class NewBase does not need to specify any exceptions. v)There is no problem with the code ------are correct
But my question is since IOException is a checked exception it needs to be handled. So the option iii) method1 in class NewBase must at least give IOException in its throws clause. ----is correct.
When i ran this program in Eclipse it works fine but when i add main() method- public static void main(String[] args){
Base b = new Base ();
b.method1();
} then it shows error as unhandled type IOException
So do i have to analyse the code as given or analyse it after adding main() method.
Please advise!