Throwables.propagate(e) 是否需要使用 throw 关键字?

Is it necessary to use throw keyword with Throwables.propagate(e)?

在此代码中,是否需要 throw 关键字来传播异常?

try {
  //try something
} catch (Exception e) {
  throw Throwables.propagate(e);
}

Throwables 文档说此方法总是抛出异常 - 添加 throw 是多余的吗?我可以改写以下内容吗?

try {
  //try something
} catch (Exception e) {
  Throwables.propagate(e);
}

javadoc 还指出

The RuntimeException return type is only for client code to make Java type system happy in case a return value is required by the enclosing method.

然后提供这个例子

T doSomething() {
  try {
    return someMethodThatCouldThrowAnything();
  } catch (IKnowWhatToDoWithThisException e) {
    return handle(e);
  } catch (Throwable t) {
    throw Throwables.propagate(t);
  }
} 

换句话说,return 类型 (RuntimeException) 是必需的,因此您可以在 returnthrows 语句中使用该方法。

在上面的例子中,如果你在最后一个catch块中省略了throw,那么Java编译器会报错,因为它不能保证一个值是return 从那个 catch 块编辑。 A return or throws instead indicate that the method completes at that point, abruptly.