【发布时间】:2015-07-04 11:06:23
【问题描述】:
在主要使用 Python 和 C/C++ 工作几年后,我回到了 Java。我经常在其他结构中使用asserts。今天我很难让自己不使用断言。
$ java -help 2>&1 | grep -2 "\-ea"
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
我的团队在任何情况下都不使用-ea,所以我使用如下结构:
assert COND;
if (! COND) {
throw new AssertionError("COND was not true");
}
我只想把assert COND;放在哪里。
经常喜欢:
assert variable != null;
if (variable == null) {
throw new AssertionError("variable was null");
}
我只想把assert variable != null;放在哪里。
您对此有何评论或如何改进?
【问题讨论】:
-
public void assert(boolean condition, final String message) throws AssertionError {if (!cond) throw new AssertionError(message);} call as assert(some condition, "some message");跨度>
标签: java assert jvm-arguments assertion