Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Sat Jun 15, 2013 7:43 pm
by deepa.patre
In this program,

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!

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Sat Jun 15, 2013 8:43 pm
by admin
You have to analyse the question exactly as given and as per the information given in the problem statement.

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Tue Aug 04, 2015 12:06 pm
by Eugeny
The option "There is no problem with the code" should be correct. What is the problem with the code?
It is compiled without any error.

The overriding method may also choose NOT to throw any exception (even though the override method declares any to throw), isn't it?

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Tue Aug 04, 2015 12:45 pm
by admin
That is indeed the correct answer.

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Mon Sep 07, 2015 8:11 pm
by shs174
method1 of Base class has a commented line saying "some I/O operations". I/O operations require us to handle IOException or to declare in the method signature. So wouldn't the option "method1 in class NewBase must at least give IOException in its throws clause" be correct?

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Mon Sep 07, 2015 9:16 pm
by admin
Yes, but that is not the method that is being invoked in NewBase's method1.

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Tue Nov 03, 2015 8:22 am
by Mushfiq Mammadov
Maybe you intend to write NullPointerException instead of NullpointerException
Image

Re: About Question enthuware.ocajp.i.v7.2.1093 :

Posted: Tue Nov 03, 2015 8:41 am
by admin
Fixed.
thank you for your feedback!