About Question enthuware.ocpjp.v8.2.1128 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
uvsmtid
Posts: 6
Joined: Sat Jun 18, 2016 2:11 am
Contact:

About Question enthuware.ocpjp.v8.2.1128 :

Post by uvsmtid »

This question is not clarified enough.

Question
Overriding method cannot throw an exception that is not thrown by the overridden method.
Explanation
If the base class A has: void m1() throws IOException then overriding method in class B can be : void m1() throws FileNotFoundException because FileNotFoundException is a subclass of IOException.
  • If is-a relationship is considered, then throwing FileNotFoundException by overriding method is like throwing IOException. In that case, overriding method actually complies with throwing only IOException.
  • Throwing anything <? extends RuntimeException> is also always possible. Always. And, technically, it is thrown by the overridden method as well.
This way makes answer True correct.

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

Re: About Question enthuware.ocpjp.v8.2.1128 :

Post by admin »

1. FNFE is an IOE, not the other way round. The overridden method throws IOE. It cannot be said that since overridden method throws IOE, it throws FNFE.

Consider the following code:

Code: Select all

class A{
 void m1() throws IOE{ }
}

class AA extends A{
  void m1() throws FNFE{ }
}

try{
 AA aa = new AA(); //Notice the type of the variable aa.
}catch(FNFE fe){} 
This catch will work fine if AA's m1 declares throws FNFE but will not work if it declares throw IOE. This proves that throwing FileNotFoundException by overriding method is not like throwing IOException.

2. Now, regarding RTEs: The question is asking about the exception thrown by a method. Not about the throws clause. An overriding method is allowed to actually throw any RuntimeException irrespective of whether the overridden method throws it or not.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

msg45f
Posts: 2
Joined: Sat May 26, 2018 12:54 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1128 :

Post by msg45f »

The wording of this question really seems to be open for interpretation and seems like a departure from the general interpretation of explanations.

Given that emphasis seems to be defined based on contract rather than implementation, it seems reasonable assume that a method declaring that it throws IOException includes in its declaration all extending exceptions. Regardless of implementation, it would be permissible for any method declaring to throw IOException to throw FNFE. Any extending class can then override the method and declare itself to throw IOException or any more specific exception of type IOException.

The real heart of the question seems to be checking that one understands that an overriding method can't throw an exception that is unrelated to or broader than the overridden method, as this would result in method calls from polymorphic references throwing unanticipated exceptions. Unless this specific wording is what we should be prepared for, I think the wording could be clarified that it is referring to what the implementation throws rather than what the contract declares could be thrown.

floryan
Posts: 17
Joined: Sun Sep 23, 2018 4:10 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1128 :

Post by floryan »

msg45f wrote:
Sat May 26, 2018 1:17 pm
The wording of this question really seems to be open for interpretation and seems like a departure from the general interpretation of explanations.

Given that emphasis seems to be defined based on contract rather than implementation, it seems reasonable assume that a method declaring that it throws IOException includes in its declaration all extending exceptions. Regardless of implementation, it would be permissible for any method declaring to throw IOException to throw FNFE. Any extending class can then override the method and declare itself to throw IOException or any more specific exception of type IOException.

The real heart of the question seems to be checking that one understands that an overriding method can't throw an exception that is unrelated to or broader than the overridden method, as this would result in method calls from polymorphic references throwing unanticipated exceptions. Unless this specific wording is what we should be prepared for, I think the wording could be clarified that it is referring to what the implementation throws rather than what the contract declares could be thrown.
I absolutely agree. The question's wording should be made more specific.

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1128 :

Post by bvrulez »

The issue is if this kind of question will be in the exam or not. Also, just by reading the question more formally it will get clear:

"Overriding method cannot throw a java.lang.Exception that is not thrown by the overridden method."

Yes it can throw java.lang.RuntimeException since RuntimeException IS A java.lang.Exception (but might not be declared in a throws clause by the overridden method."

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

Re: About Question enthuware.ocpjp.v8.2.1128 :

Post by admin »

Yes, it could very well be on the exam.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: alexandrapr and 31 guests