Java断言打印

son*_*nia 3 java eclipse assert

当条件在eclipse中为真时,我需要在断言中将语句打印到控制台.怎么样?

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 ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*oun 5

你需要:

  • 选择RunRun Configuration →右键单击Java ApplicationNew →Go to Arguments选项卡→ 在VM参数中写入-ea:

在此输入图像描述

现在,当你有类似的东西:

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

你会在控制台中看到:

Exception in thread "main" java.lang.AssertionError: Error!!!
    at .....
Run Code Online (Sandbox Code Playgroud)