• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding assertion

 
Suguna Gollapally
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {

private Boolean isValid(int i) {

if(i<1 || i>12)
return new Boolean("Incorrect value");

else
return new Boolean(true);
}

private void testValue(int val) {

assert isValid(val) : "Out of range";
}
public void static main(String s[]) {

Test t=new Test();
t.testValue(22);
}
}

why is this prog doesn't throw assertion error even though assert(isValid) is not true
 
Loveen Jain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you turned on the assertions while running the program.

Try this:
java -ea <class-name>

I tried to run this code and got the following Exception

Exception in thread "main" java.lang.AssertionError: Out of range
at com.javaranch.AssertTest.testValue(AssertTest.java:13)
at com.javaranch.AssertTest.main(AssertTest.java:17)
 
Suguna Gollapally
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jain..

Even i got the same output when i enabled the assertions..
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic