aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Master exam question on assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Master exam question on assertion" Watch "Master exam question on assertion" New topic
Author

Master exam question on assertion

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
A master exam question asks to choose true statements but
in the answer the statement
"Never throw an assertion error explicitly"

is marked incorrect.According to me it is a correct statement as we
should neither try to throw nor catch assertion error explicitly
Correct me if i am wrong....


SCJP 6
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi Ujjawal,

it's recommended not to throw an AssertionError explicity.

Since AssertionError is thrown when a statement's boolean test return false and since AssertionError is a subclass from java.lang.Error:

java.lang.Object
--java.lang.Throwable
----java.lang.Error
------java.lang.AssertionError

You can thus throw java.lang.AssertionError as it derives from java.lang.Throwable:

throw new AssertionError();
is nearly the same as:
assert false;

I say "nearly" because the second statement only has effect when assertions are enabled;
if assertions are disabled, the second statement is not even seen by the JVM.

As the name "assertion" says, you assert that s.th. won't come into being (for example, a code fragment won't never be reached) - in order to check this, you can enable assertions while developing.

But after deployment, all things concerning assertions don't have to be an issue any more, so it's not good practice throwing an AssertionError explicity - because in such case, you can't disable a "throw-statement" in a future productive environment.


SCJP 6 (88%), SCWCD (89%)
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Nice Explanation but i want to know that the master exam question which i quoited is the right answer or not..
Please let me know
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

ujjawal rohra wrote:Nice Explanation but i want to know that the master exam question which i quoited is the right answer or not..
Please let me know

Hi Ujjawal,
"Never throw an assertion error explicitly"
should be true. Please check the errata for K&B book if this is there


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rajeev Rnair wrote:
"Never throw an assertion error explicitly"
should be true.


No. it is not . you can validate and still throw an AssertionError from *private* method which is not directly called by a client.
but it is not a good idea to throw that(AssertionError which is not understand by user/client) from public method

hth
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308



No. it is not . you can validate and still throw an AssertionError from *private* method which is not directly called by a client.
but it is not a good idea to throw that(AssertionError which is not understand by user/client) from public method

hth


Why not use "assert false " rather than "throw AssertionError"? If assertions are disabled the code wouldn't execute where as if you use "throw new AssertionError()", disabling assertions wouldn't prevent the error message from kicking in.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

it is not about enabling or disabling assert ; it is about *throw* an AssertionError .
I saw the throw statement in java API many places, example *java.math.BigDecimal* ; infact by throwing explicitly, you no need to care about to enabling assertion.

normally, when precondition or postcondition vallidation fail in a private method, you can throw an AssertionError.
the below link discussion is bit different from what i said; but anyway it will help to the Original Poster

another discussion on the same

hth
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Check this discussion also!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hmmm..Rajeev , Ok .

api .

from above link

Thrown to indicate that an assertion has failed.


it didnt mention that you should not throw explicitly . but yes, i am looking some more posts on this
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Can you post the ENTIRE question from the master exam?

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
The entire question from the MasterExam is:


Which are true? (Choose all that apply.)
A It is not good practice to place assertions where you think execution should never reach.
B It is sometimes appropriateto call getters and setters from assertions.
C Use assertions to verify the arguments of private methods.
D Assertions can be disabled for a particular class.
E Never throw an AssertionError explicitly.


The reported correct answer is C & D. I also think E should be a correct answer. The explanation for why E is not a correct answer appears to be missing. The Reference simply says:

E is also incorrect.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Ryan Slominski wrote:I also think E should be a correct answer.


Hello Ryan...For curiosity why do you think E should be a correct answer?.

If the sentence were like this: "It is appropriate NEVER to throw an assertion error" one might consider it, because the "appropriate" thing to do is to "assert" a situation that may result in true or false. IMHO you can explicitly throw an Assertion error if a conditional test does NOT satisfy your requirement.

Regards

Ikpefua

OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
It's a very poorly constructed question. Only three of the choices (A, B and D) are assertive statements which can be true or false. The other two, C and E, are imperatives which can be neither true nor false, in exactly the same way that the command "open the door" can be neither true nor false. It surprises and disheartens me to encounter this kind of error.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Dennis Deems wrote:It's a very poorly constructed question. Only three of the choices (A, B and D) are assertive statements which can be true or false. The other two, C and E, are imperatives which can be neither true nor false, in exactly the same way that the command "open the door" can be neither true nor false. It surprises and disheartens me to encounter this kind of error.


Hello Dennis...I respect your criticism, and it is true that the main question itself has a certain degree of "ambiguity", the fact is that writing a computer programming language exam book that has approximately 851pages WITHOUT errors is humanly impossible. That said I think the best way to have asked the question w'd have been as follows:

"Considering the appropriate use of assertions, which of the following are true?"

Regards

Ikpefua.
Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
Ikpefua Jacob-Obinyan wrote:why do you think E should be a correct answer


You should never explicitly throw an AssertionError because:

  • You can just use the assert statement (which will delegate to the JVM to throw the Error on your behalf if the condition is met and assertions are enabled)
  • General best practice says you shouldn't throw an Error (The Java Tutorial says this for example: http://download.oracle.com/javase/tutorial/essential/exceptions/throwing.html).


  • Dennis Deems wrote:C and E, are imperatives which can be neither true nor false


    Nice observation. Now that you point it out, it IS a poorly worded question! Even still, I think I understand what C and E are "saying", and I think E should be true.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Master exam question on assertion