Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ");
        }
    }
}
like image 921
sonia Avatar asked Nov 01 '25 14:11

sonia


1 Answers

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 .....
like image 125
Maroun Avatar answered Nov 03 '25 04:11

Maroun