Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding assertion" Watch "Regarding assertion" New topic

Regarding assertion

Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
public class Test {

private Boolean isValid(int i) {

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

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();

why is this prog doesn't throw assertion error even though assert(isValid) is not true
Loveen Jain
Ranch Hand

Joined: Jan 24, 2004
Posts: 35
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(
at com.javaranch.AssertTest.main(
Suguna Gollapally
Ranch Hand

Joined: Oct 31, 2006
Posts: 37
Thanks jain..

Even i got the same output when i enabled the assertions..
subject: Regarding assertion
It's not a secret anymore!