2014-01-18 74 views
1

我已經在其他地方發佈了這個,沒有解決方案,所以也在這裏發佈。由於num小於5,下面提到的代碼沒有像我期望的那樣拋出斷言錯誤。希望有人能提供建議。謝謝。斷言錯誤問題

public class Wrong { 
public static void main(String[] args) {  
    Wrong wrong = new Wrong();    
    wrong.methodE(3);     
    }  
    //AssertionError 
    void methodE(int num) 
    { 
     assert(num>5); 
    } 
} 
+0

答案,但對於更多的'assert'在Java中檢查出這個線程:http://stackoverflow.com/questions/2758224/assertion-in-java – Durandal

回答

1

我猜你忘了enable assertions

-ea參數運行jvm。

java -ea ... 

您還應該考慮提供斷言錯誤消息,例如,

assert num > 5 : "arg num must be greater than 5"; 
1

如果您使用的是Eclipse去下面介紹如何解決您的特定問題Run--> Run Configuration --> VM Argument ---> Type -ea.

+0

感謝各位快速回復。 Vinayak,我正在使用eclipse並且你的解決方案能夠工作。謝謝。 –