1.java代码
public class Test {
/**
* 开启 assert
* 1. 菜单 run
* 2. run configurations
* 3. 选择 Arguments
* 4. VM arguments 文本框中输入: -ea 表示开启 -da 表示禁止断言。
* @param args
*/
public static void main(String[] args) {
String a="abcdefg";
String b="abcdefg";
String c=new String(a);
try {
assert (a==b):"运行错误";
System.out.println("a==b");
} catch (Exception e) {
// TODO: handle exception
}
try {
assert (a==c):"运行错误";
System.out.println("a==c");
} catch (Exception e) {
//System.out.println(e.getMessage());
}
}
}
2.运行如下:
a==b
Exception in thread "main" java.lang.AssertionError: 运行错误
at Test.main(Test.java:21)