0

I have just been looking into assert in Java and now I am trying to figure out how can we view the message if the assert was true in Eclipse. Code below:

public class TheAssertClass {

    public TheAssertClass(){

        int y = 5;
        int x = 4;

        assert (y > x): "y is too big. y = " + y;
    }

    public static void main(String[] args){
        TheAssertClass go = new TheAssertClass();

        System.out.println("The program ran");
    }

}

The console only displays "The program ran". I am not exactly sure if I am using this correctly?

Thanks.

jonprasetyo
  • 3,356
  • 3
  • 32
  • 48
  • You're supposed to assert things that are *supposed* to be true. Here you'd want to assert `y < x` to trigger an error. – Chris Hayes Sep 11 '14 at 04:06
  • But isn't y is greater than x (y>x) so it is true? – jonprasetyo Sep 11 '14 at 04:09
  • Yes, `y > x` is true, which is good. You are *asserting* that `y > x`. In effect, the `assert` statement says "if this is ever not true, end my program". – Chris Hayes Sep 11 '14 at 04:10
  • You aren't meant to see the assertion message if it passes. It would just be noise. – Jon Skeet Sep 11 '14 at 04:11
  • possible duplicate of [How to enable the Java keyword assert in Eclipse program-wise?](http://stackoverflow.com/questions/11415160/how-to-enable-the-java-keyword-assert-in-eclipse-program-wise) – Michael Petch Sep 11 '14 at 04:18
  • hmmm not really a duplicate, I didn't know that I had to enable something. – jonprasetyo Sep 11 '14 at 04:23

2 Answers2

2

You might not have enabled assertions at the JVM level (with the -ea switch). If you haven't, then this would explain the behaviour you are seeing. Try making the assertion false and see if anything appears on the console.

Also have a look at the following questions & answers for more detail: * See What does the "assert" keyword do?

Community
  • 1
  • 1
roj
  • 1,262
  • 13
  • 27
  • 1
    Thanks I have enabled the vm arguments "-ea" in the run configuration in Eclipse and now it works. – jonprasetyo Sep 11 '14 at 04:22
  • However it thows an exception which is what I am suppose to get right? (if it returns false) – jonprasetyo Sep 11 '14 at 04:22
  • Maybe... I'll explain. It should throw an AssertionError (a subclass of Throwable) which would also give a stack trace. Exceptions are also subclasses of Throwable but are siblings of Errors. All derivatives of Throwable can contain stack traces. So I suspect you saw a stack trace and are therefore calling it an exception, whereas you in fact got an Assertion**Error**. If you got an AssertionError, then yes that is correct. If you got some other Exception then no, you something else has happened. What is the exception? So it depends on what you actually got, hence the 'Maybe'! – roj Sep 11 '14 at 21:46
0

Info on how to enable assertion in eclipse:

Run -> Run Configuration -> Java Application -> Arguments (2nd tab) -> VM arguments -> -ea

-ea = enable assertion.

vmorusu
  • 936
  • 1
  • 15
  • 32