• 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

Regarding assertion

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jain..

Even i got the same output when i enabled the assertions..
 
reply
    Bookmark Topic Watch Topic
  • New Topic