• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding assertion mechanism

 
Mckenzie John
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have a doubt regarding the assertion mechanism. Let me explain:

// code
assert( value >= 0 && value <= 20 ) : "Invalid number: " + value;
// coding continues

In the above scenario,

1) when is the assertionError thrown? when the condition is false - Is the value is printed as similar to an SOP along with the stackTrace?

2) If the condition is true , say the relational operator returns true , the coding continues without printing the value . Is my understanding correct?

3) In SCJP real exam, what answer must i choose if i get a question like the above is asked - what is the output of an assert statement - without mentioning anything about it being enabled or disabled? I know that assertions are disabled by default. So should i assume that even though assertion is present it has no use as assertion is not enabled and mark the choice that the coding proceeds normally?


Thanks in Advance.

Regards,
Mck
 
nir sharma
Ranch Hand
Posts: 72
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your first and second statements are correct. And you can run your program to see the exact stacktrace.
In regard to the third statement I think you should consider that the assertion is enabled however I would like other ranchers to back my statement.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mckenzie John wrote:
1) when is the assertionError thrown? when the condition is false - Is the value is printed as similar to an SOP along with the stackTrace?


the above one is interpreted by compiler as in

So, that means you are passing the error message to AssertionError's constructor . you can get the message using getMessage() in future.

2) If the condition is true , say the relational operator returns true ,
the coding continues without printing the value . Is my understanding correct?

Yes, with out throwing an Error.

3) In SCJP real exam, what answer must i choose if i get a question like the above is asked - what is the output of an assert statement - without mentioning anything about it being enabled or disabled? I know that assertions are disabled by default. So should i assume that even though assertion is present it has no use as assertion is not enabled and mark the choice that the coding proceeds normally?


Here I am not sure. hope, some other rancher can help you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic