先打開相應的配置面板,有以下兩種方式。
然后在VM欄里輸入 -enableassertions 或者 -ea 就好了
然后編寫程序試試
我的目錄結構如下:(因為Main class那里要寫類的全限定名, -ea的上一行)
代碼:
public class AssertTest { public static void main(String[] args) { int a = 10; int b = 20; assert a > b : "錯誤,a不大於b"; } }
結果:
如果不寫-ea,則程序繼續執行,不檢測assert斷言,不會報異常.