标记线的更好方法是永远不会达到的?

时间:2017-10-30 13:59:33

标签: java assertion dead-code

假设此代码段:

  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

2 个答案:

答案 0 :(得分:3)

将其替换为IllegalStateException,替换

assert false; 

throw new IllegalStateException("JVM is still running after calling halt");

答案 1 :(得分:0)

您可以抛出RuntimeException