About Question com.enthuware.ets.scjp.v6.2.649 :
Moderator: admin
-
- Posts: 7
- Joined: Wed Dec 04, 2013 5:07 pm
- Contact:
About Question com.enthuware.ets.scjp.v6.2.649 :
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!
"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!
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
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.
Since you have to select one option, you need to select the best option.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Mar 02, 2015 8:56 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
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.
-
- Posts: 3
- Joined: Sat Feb 27, 2016 1:55 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
Can you please explain why option 1 is wrong? Thanks.
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
Please read the discussion above.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Dec 14, 2020 8:53 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
I am confused...
And then...
The following snippet compiles:Overriding method cannot throw IOException
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();
}
}
Since IOException is a checked exception and is not a subclass of NullPointerException, the overriding method cannot throw this exception.
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Dec 14, 2020 8:53 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.649 :
You are right! My IDE did not show any warning... 
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.

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.
Who is online
Users browsing this forum: No registered users and 4 guests