sonia
sonia

Reputation: 405

Java assert printing

I need to print the statement in the assertion to the console when condition is true in eclipse. How?

public static void main(String[] args) {
    try {
        assert(args[0].equals("x")): "kate";
    } catch(Error e) {
        System.out.print("ae ");
    } finally {
        try {
             assert(args[0].equals("y")): "jane";
        } catch(Exception e2) {
             System.out.print("ae2 ");
        }
    }
}

Upvotes: 4

Views: 3099

Answers (1)

Maroun
Maroun

Reputation: 95948

You need to:

  • Select RunRun Configuration → Right click on Java ApplicationNew → Go to Arguments tab → Write -ea in VM arguments:

enter image description here

Now, when you have something like:

assert(1==2) : "Error!!!";

You'll see in the console:

Exception in thread "main" java.lang.AssertionError: Error!!!
    at .....

Upvotes: 6

Related Questions