• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java 1.4 assertions question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason I can't get asserts to work in this program i am writing, I was just wondering if the code I give after the current code will work instead?


-This uses asserts as you can see...




As opposed to this , notice the if statement.....




-Thanks, Kevin
[ April 12, 2005: Message edited by: Kevin Peterson ]
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you compile and execute the code? Did you compile and execute it by specifying the "enable assertion" option?

Nick
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, it's generally not a good idea to use assertions for checking the preconditions of public methods or constructors. You should throw an IllegalArgumentException (run-time exception) instead, so that the public contract is enforced whether assertions are turned on or not.

Use assertions for checking the preconditions of non-public methods, or for checking internal consistency and post-conditions.
 
David Peterson
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


To answer your original question, to run the code with assertions use the -ea (or -enableassertions) switch when you run java.

 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea I understand but i compiled with with this above my code:



and got a compile time error. I was just wondering if there was a simple if statement that I stated above that would work?

-Kevin
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actully throwing an IllegalArgumentException() works fine, Thanks David.
-Kevin
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic