返回 null 和抛出异常之间有什么区别?

huangapple go评论80阅读模式
英文:

what is different between return null and throw exception?

问题

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

这个方法的返回值是一个 IBinder,但实际上它只是抛出了一个异常。我的问题不是关于服务的,而是为什么这个方法没有显示编译错误?

如果我们写上 return null(如果我们不想实现这个方法),或者写上 throw exception,有什么不同呢?

英文:
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

This method has an IBinder as return value, but it just throws an exception, my question is not about services, my question is that why this method does not show a compiler error?

and what is different if we write return null( if we do not want to implement this method) or we write throw exception?

答案1

得分: 3

通常,抛出异常更优,因为它比返回null提供了更多信息,而且调用代码不能只是漠视返回值,从而导致随后的、无信息的NullPointerException。

然而,onBind的文档明确表示返回null是可以的。因为API就是这样设计的,在这种特定情况下,与其抛出异常,返回null更加合理。

总的来说,抛出异常更好,因为它可以阻止调用代码假设操作成功,从而继续进行,而实际上操作并未成功。但在这种情况下,当操作不受支持时,该方法实际上应返回null(在我看来,这不是一个好的设计决策)。

英文:

Normally, throwing an exception is superior because it is considerably more informative than returning null, and because calling code can’t just blithely ignore the returned value in a way that leads to a subsequent and uninformative NullPointerException.

However, the documentation for onBind explicitly says that it’s okay to return null. Since that’s how the API is designed, it makes more sense to return null in this particular case than to throw an exception.

In general, throwing an exception is better, because it prevents calling code from assuming it’s okay to continue as if the operation succeeded when it did not in fact succeed. But in this case, the method is actually supposed to return null when the operation is not supported (which in my opinion is not a good design decision).

答案2

得分: 2

return null 返回一个空值。throw new 无论你抛出什么异常,都会中止方法的执行并引发指定类型的异常。在这种情况下,抛出异常不会显示编译错误,因为如果那是你唯一要做的事情,你永远不会返回任何东西,这也是有效的。例如,如果异常是在if/else的分支中抛出的,你需要在另一个分支中抛出异常或返回一个值,否则编译器会报错,告诉你必须返回类型为IBinder的值。

英文:

return null returns a null value. throw new whatever exception you throw aborts the execution of the method and raises an exception of the specified type. Throwing an exception in this case does not show a compiler error because if that's the only thing you do, you're never going to return anything, which is also valid. For example, if the exception was thrown in a branch of an if/else, you'd need to either throw an exception or return a value in the other branch, or you'd get the compiler error telling you that you must return a value of type IBinder.

huangapple
  • 本文由 发表于 2020年9月1日 06:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63678869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定