This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

assert?  RSS feed

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't assert throw an AssertionError?


Sourced from a tutorial Programming with Assertions.

(Edited back to the original version so that the response doesn't look like a WTF -- Paul C)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you expect it to? The expression is true.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Why would you expect it to? The expression is true.


Sorry, I was scribbling too fast, it still does not work with assert (y==10), so I'm wondering whether I need to add something to the code; do I need to add something to the class declaration?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK.

You need to run it with assertions turned on at runtime - they're disabled by default. That's done with a command-line flag for the java command.

So:

java -ea ClassName

(ea stands for enable assertions)

It might be worth keeping it really simple: add an assert false right at the beginning of the main method. That way, once you've succeeded in getting assertions enabled you'll know right away, and won't have to worry about whether it's a logical problem in your code.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!