当 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 ");
}
}
}
最佳答案
你需要:
- 选择运行 → 运行配置 → 右键单击Java 应用程序 → 新建 → 转到参数选项卡→在VM参数中写入-ea:
现在,当你有这样的事情时:
assert(1==2) : "错误!!!";
您将在控制台中看到:
Exception in thread "main" java.lang.AssertionError: Error!!!
at .....
关于Java断言打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15585257/