一种更好的方法来标记一条永远不会到达的线?
A better way to mark a line, that will never be reached?
假设这段代码:
public synchronized void kill() {
log.info("Killing {}...", this);
Runtime.getRuntime().halt(HALT_STATUS_CODE);
assert false; // Line should never reach this point!
}
因为断言取决于断言参数是否启用。有没有更好的方法来标记和检查一条在执行时永远不会到达的线? (不仅针对此代码段)
断言文档:https://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html
你可以扔 RuntimeException
.
将其替换为 IllegalStateException,替换
assert false;
和
throw new IllegalStateException("JVM is still running after calling halt");
假设这段代码:
public synchronized void kill() {
log.info("Killing {}...", this);
Runtime.getRuntime().halt(HALT_STATUS_CODE);
assert false; // Line should never reach this point!
}
因为断言取决于断言参数是否启用。有没有更好的方法来标记和检查一条在执行时永远不会到达的线? (不仅针对此代码段)
断言文档:https://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html
你可以扔 RuntimeException
.
将其替换为 IllegalStateException,替换
assert false;
和
throw new IllegalStateException("JVM is still running after calling halt");