• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Understanding assertion mechanism

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic