• Post Reply Bookmark Topic Watch Topic
  • New Topic

assert() function is not working. Why?  RSS feed

 
Hasan Fatih
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following program is not working as I want. The assert() function is not working while I'm entering a number which is out of range 0-10. Whereas, according to my book, it is working like that:

Output:
Enter a number between 0 and 10: 50
Exception in thread "main" java.lang.AssertionError: bad number: 50
at AssertionTest.main(AssertTest.java:15)


But I cannot see the error message above after compiling the program on eclipse and netbeans. I just see that:

Output:
Enter a number between 0 and 10: 50
You entered 50

What is the problem? Why the function assert is not working?

 
Hasan Fatih
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learnt answer in another forum:


assert is not a function, its a keyword. Its a build in feature that must be enabled before it can be used.

To enable this feature you have to add the command "-ea" to the argument list when initializing your JVM.
Your IDE should give you the option to set a Run-Configuration where you can select the JVM commands to be used when testing your application. Make sure to add the command to the correct list

Source of Answer

And I could do it like that:

In Eclipse:
Run->Run Configurations->Arguments->VM Arguments-> ....
I wrote -ea in VM Arguments.

In Netbeans:
Run->Set Project Configuration->Customize->VM Options->...
I wrote -ea in VM Options.

Although there is not any answer, thank you every reader of this topic.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting the answer, that will be useful for people who are searching for an answer for this question in the future.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!