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

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
ETS User

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

Post by ETS User »

Why will the first code throw AssertionError and not Exception.

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

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

Post by admin »

There only 4 members in the given enum and there is a case for each of them. Therefore, there is no way the control can enter the default block. And that is what you are trying to assert.

Throwing an Exception is not appropriate because you are not validating input parameters here. If you had only 3 cases, and if you didn't want any one to pass the 4th value, then throwing an exception in that case would be appropriate.

HTH,
Paul.

zzantonni

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

Post by zzantonni »

Hi Paul,
from the question i don't understand that those are THE ONLY 4 valid members, i just see that those are 4 of the valid memebrs.

What if the enum has also a JOLLY member, and JOLLY should not be allowed in that piece of code?

I think IllegalArgumentException should be more appropriate in that scenario.

Thanks!

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

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

Post by admin »

That is a very good point, zantonni. You are right, in that case IllegalArgumentException would be more appropriate. The question has been updated to make it clear that these are the only 4 valid values.

thank you for your feedback!
Paul.

Robyn

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

Post by Robyn »

I'm still completely confused by this question.

I have read the docs that it says to read at the bottom of the question, (in the Enthu study references section) and they state this:
"Note that the programmer does not explicitly throw AssertionError using the throw keyword."

Also - any argument which is not one of the 4 enums would surely be an illegal argument wouldn't it?

So why is the correct answer to throw an AssertionError, and not throw an IllegalArgumentException?

I can find nothing anywhere online which explains this.

Thanks

Robyn

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

Post by Robyn »

Sorry - the post above is referring to the first question on the D&D, with the switch statement..

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

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

Post by admin »

Assertions are used to verify assumptions that you know will most certainly hold true in any case.
IllegalArgument Exception is thrown explicitly to enforce the API contract. I.e. in cases when it is technically possible by the users of your class to break the contract and you want to prevent that from happening.

Thus, in this case AssertionError is more appropriate. It is not thrown explicitly but implicitly by using the assert statement.

-Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests