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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
deepa.patre
Posts: 15
Joined: Thu Dec 13, 2012 9:44 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You have to analyse the question exactly as given and as per the information given in the problem statement.

Eugeny
Posts: 3
Joined: Mon Jun 08, 2015 3:28 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That is indeed the correct answer.

shs174
Posts: 1
Joined: Mon Sep 07, 2015 8:07 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, but that is not the method that is being invoked in NewBase's method1.

Mushfiq Mammadov
Posts: 32
Joined: Fri Oct 16, 2015 4:33 am
Contact:

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

Post by Mushfiq Mammadov »

Maybe you intend to write NullPointerException instead of NullpointerException
Image

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Fixed.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests