Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Mon Feb 03, 2014 10:33 am
by chang_surrey
May I ask why option 5 is wrong?

"Overriding method may choose not to throw any exception."

I chose option 3 because class A obviously doesn't compile, but I'm curious what makes option 5 a wrong answer.

Since the original method only throws an unchecked NPE, I assume its overriding version is free to throw nothing at all? Thanks!

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Mon Feb 03, 2014 10:51 am
by admin
It is true that the overriding method may choose not to throw any exception. But that implies that the given code is fine, which it is not. That is why that option is incorrect.
Since you have to select one option, you need to select the best option.

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Mon Mar 02, 2015 8:59 am
by makemoneyy
This question is ambigious and should be avoided. Specially when multiple answer can be selected. I see the option 1, 3, and 5 are valid.

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Tue Jun 14, 2016 6:52 pm
by msalam4
Can you please explain why option 1 is wrong? Thanks.

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Tue Jun 14, 2016 9:08 pm
by admin
Please read the discussion above.

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Fri Feb 26, 2021 8:30 am
by fortesp
I am confused...
Overriding method cannot throw IOException
The following snippet compiles:

Code: Select all

class A {
    byte getStatusCode(Object obj) throws NullPointerException {
        if (obj != null) return 127;
        else return -1;
    }
}

class B extends A {
    byte getStatusCode(Object obj) throws IOException {
        if (obj != null) return 127; // changed from 128 to 127.. for the example
        else throw new IOException();
    }
}
And then...
Since IOException is a checked exception and is not a subclass of NullPointerException, the overriding method cannot throw this exception.

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Fri Feb 26, 2021 10:27 am
by admin
fortesp wrote:
Fri Feb 26, 2021 8:30 am
The following snippet compiles:
....
Are you sure?

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Posted: Fri Feb 26, 2021 11:49 am
by fortesp
You are right! My IDE did not show any warning... :x

Its funny though. If i leave just the throws clause it compiles, but if i throw the exception it does not compile. So in terms of method override, i can use throws but not throw an actual exception.