About Question enthuware.ocpjp.v7.2.1313 :
Moderator: admin
-
- Posts: 2
- Joined: Sat Jul 26, 2014 10:40 am
- Contact:
About Question enthuware.ocpjp.v7.2.1313 :
Hi, the line of code assert enabled = true; is assigning true to enabled, so there will never be any exceptions thrown. I mean, the correct answer "It will not throw anything if assertions are disabled." is wrong, because even if asssertions are enabled, nothing will be thrown. Is it right? Thanks.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1313 :
Yes, the given code is not useful and that is the point it is trying to get at.
But the statement, "It will not throw anything if assertions are disabled." is true because it states a fact. i.e. if assertions are disabled, no exception will be thrown. The statement doesn't say anything about what happens if assertions are enabled.
HTH,
Paul.
But the statement, "It will not throw anything if assertions are disabled." is true because it states a fact. i.e. if assertions are disabled, no exception will be thrown. The statement doesn't say anything about what happens if assertions are enabled.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1313 :
The code in the question:
boolean enabled = false; assert enabled = true; assert enabled; ... rest of the code ...
The answer states that it should be like:
assert enabled = true;
if( !enabled ) throw new RuntimeException("Assertions should be enabled");
but that RuntimeException can still never be thrown since enabled is always false, so while the question is correct, the explanation is far from useful stated this way since it may look like it differs from the code in the question, but that is not the case. The only difference is that "assert enabled;" has been removed, kind of odd to present that as an explanation
boolean enabled = false; assert enabled = true; assert enabled; ... rest of the code ...
The answer states that it should be like:
assert enabled = true;
if( !enabled ) throw new RuntimeException("Assertions should be enabled");
but that RuntimeException can still never be thrown since enabled is always false, so while the question is correct, the explanation is far from useful stated this way since it may look like it differs from the code in the question, but that is not the case. The only difference is that "assert enabled;" has been removed, kind of odd to present that as an explanation
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1313 :
Why would enabled be always false? If assertions are enabled, the statement assert enabled = true; sets enabled to true and the the next line if( !true) will not throw an RTE.
If the assertions are disabled, the assert enabled = true; will not execute and in that case enabled will remain false. Therefore, if( !false) will cause cause the RTE to be thrown.
If the assertions are disabled, the assert enabled = true; will not execute and in that case enabled will remain false. Therefore, if( !false) will cause cause the RTE to be thrown.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 81 guests