0

我不必在保证抛出异常的函数中指定返回值,因为该路径已死:

// Works fine!
public boolean a() {
   throw new RuntimeException();
}

我不明白为什么我不能在这里做同样的事情:

// Error: This method must return a result of type boolean
public boolean a() {
   try {
      return mayReturnOrThrow();
   }
   catch (Exception ex) {
      new RuntimeException(ex);
   }
}

我在这里想念什么?

4

1 回答 1

1

new RuntimeException(ex);只是做了一个例外,然后什么都不做 - 这是一个无用的无操作。你想扔它:

throw new RuntimeException(ex);
于 2021-01-01T14:33:57.193 回答