我应该明确抛出 NullPointerException 还是让 Java 为我做? [英] Should I throw a NullPointerException explicitly or let Java do it for me?

查看:47
本文介绍了我应该明确抛出 NullPointerException 还是让 Java 为我做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想知道关于抛出 NullPointerExceptions 的最佳实践是什么.具体来说,如果我有一个外部库函数可以在我不想实际处理的情况下返回 null(参见下面的具体示例),作为 null表示软件有问题.问题是,我应该

As the title says, I am wondering what the best practice is regarding the throwing of NullPointerExceptions. Specifically, if I have an external library function that can return null in circumstances that I don't want to actually handle (see below for a specific example), as the null indicates a problem with the software. The question is, should I

  1. 检查null的返回值并自己抛出NullPointerException,或者
  2. 我是否应该在我尝试使用该对象时就让 Java 为我代劳.
  1. check the return value for null and throw the NullPointerException myself, or
  2. should I just let Java do the dirty work for me as soon as I try to use the object.

第一种方法让我添加一些额外的信息,因为我可以构造 NullPointerException,但在我看来,第二种方法可以使代码更清晰.我还想知道任何性能影响,即 Java 在本机"抛出 NPE 方面是否更有效?

The first approach lets me add some additional information, since I get to construct the NullPointerException, but the second makes for cleaner code in my opinion. I would also wonder as to any performance implications, that is, is Java more efficient at throwing the NPE "natively"?

举例来说,我正在尝试使用 Java Speech API 使用以下代码创建语音合成器:

By way of example, I am trying to use the Java Speech API to create a speech synthesizer using the following code:

synthesizer = Central.createSynthesizer(generalDesc);

if (synthesizer == null) {
    // (1) throw NPE explicitly
    throw new NullPointerException("No general domain synthesizer found.");
}

// (2) let the JVM throw the NPE when dereferencing
synthesizer.allocate();

如果

Central.createSynthesizer 找不到合适的合成器,则返回null,这通常是由于缺少speech.properties 文件造成的.因此,这是系统设置错误的问题,并且在运行时无法恢复,而不是需要以编程方式处理的情况.因此,我相信抛出 NullPointerException 是一个有效的响应,因为它表明一个错误(不是在代码中,而是在软件部署中).但是由于 synthesizer 对象在下一个语句中被取消引用,我是否应该让 JVM 为我抛出 NPE 并保存空检查?

Central.createSynthesizer returns null if it cannot find a suitable synthesizer, which is most often caused by a missing speech.properties file. So it's a matter of wrong setup of the system, and pretty unrecoverable from at runtime, rather than circumstances that need to be handled programmatically. As such, I believe throwing a NullPointerException is a valid response, since it indicates a bug (not in the code but in the deployment of the software). But since the synthesizer object is dereferenced in the very next statement, should I just let the JVM throw the NPE for me and save the null check?

附录: 考虑到 Speech.properties 在 JVM 启动时加载 需要存在于(通常)user.home"或java.home"中的文件系统上/lib",令人费解的是 createSynthesizer 在找不到 NPE 而是返回 null 时,并没有直接抛出 NPE(这是我最初在弗洛伊德的单据中写的).我认为在这里抛出 NullPointerException 是正确的做法,因为它表明软件部署中存在实际错误.

Addendum: Considering that speech.properties gets loaded when the JVM starts needs to exist on the filesystem in (normally) "user.home" or "java.home/lib", it is puzzling that createSynthesizer doesn't straight up throw an NPE (which is what I had written originally in a Freudian slip) when it fails to find it but returns null instead. I think that throwing a NullPointerException is the right thing to do here, because it indicates an actual bug in the deployment of the software.

推荐答案

在你的情况下:两者都不是.检查 null 并抛出更有意义的异常,而不是 NPE.

In your case: neither. Check for null and throw more meaningful exception, not NPE.

一般来说 - 如果不应该发生 NPE,不要明确测试它,Java 会为你做.编写的测试更少,阅读的代码更少,分析的复杂性更低.

In general - if NPE should not occur, don't test for it explicitly, Java will do it for you. Less tests to write, less code to read, less complexity to analyze.

然而,如果 null 是预期的,请尽快测试并相应地解释.否则 NullPointerException 将在稍后的不同行/方法中发生,使调试真正的问题变得更加困难.

However if null is expected test it as soon as possible and interpret accordingly. Otherwise NullPointerException will occur somewhere later in different line/method, making it harder to debug the real problem.

这篇关于我应该明确抛出 NullPointerException 还是让 Java 为我做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆