我寫API,所以我的API將從外部模塊使用。這裏是我想不出用什麼斷言或java.lang.IllegalArgumentException
斷言VS運行時異常
/**
* Adds translation of information to underlying store for particular language
* @param languageId The identifier of the language
* @param translation The translation provided for the specific language
* @throws AssertionError if the provided language id is {@code null} or empty
* or provided translation is {@code null} or empty
*/
public final void addTranslation(String languageId, String translation){
assert !(Strings.isNullOrEmpty(languageId));
assert !(Strings.isNullOrEmpty(translation));
translations.put(languageId, translation);
}
如果我使用運行時異常,我認爲這可能會損害其使用該API的應用程序的執行的方法之一。如果我使用斷言,那麼它會傷害我的API,如果斷言標誌被禁用。
也試過讀取類似的帖子When to use an assertion and when to use an exception。但是,檢測哪個案例是我的,這有點令人困惑。
有嚴格定義的方式,其中使用斷言何地使用運行時異常?
[異常Vs的斷言]的可能的複製(http://stackoverflow.com/questions/1276308/exception-vs-assertion) – emlai 2015-12-20 10:32:48