permaculture playing cards*
The moose likes Beginning Java and the fly likes java 1.4 assertions question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java 1.4 assertions question" Watch "java 1.4 assertions question" New topic
Author

java 1.4 assertions question

Kevin Peterson
Ranch Hand

Joined: Mar 05, 2005
Posts: 37
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 ]
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
How did you compile and execute the code? Did you compile and execute it by specifying the "enable assertion" option?

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
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
Ranch Hand

Joined: Oct 14, 2001
Posts: 154


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

Joined: Mar 05, 2005
Posts: 37
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

Joined: Mar 05, 2005
Posts: 37
Actully throwing an IllegalArgumentException() works fine, Thanks David.
-Kevin
 
 
subject: java 1.4 assertions question
 
Similar Threads
Using GridLayout as GUI
SoDuko puzzle
Battleships - guys please help me out!
Place A Word In A 2d Array
Java Help