• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Regarding assertion

 
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
 
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..
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic