空检查错误消息为“is null”或“为空”

发布于 2024-09-05 10:21:33 字数 333 浏览 11 评论 0原文

在 Java 代码中进行 null 检查时,如果您为 null 值抛出 IllegalArgumentExceptions,您使用哪种消息模板?

我们倾向于使用这样的东西

public User getUser(String username){
   if (username == null){
     throw new IllegalArgumentException("username is null");   
   }
   // ...
}

:“is null”或“was null”哪个更好,为什么?

对我来说“为空”感觉更自然。

When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use?

We tend to use something like this

public User getUser(String username){
   if (username == null){
     throw new IllegalArgumentException("username is null");   
   }
   // ...
}

What is better : "is null" or "was null", and why?

For me "is null" feels more natural.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漆黑的白昼 2024-09-12 10:21:33

由于 Exception 是由于前置条件检查失败而引发的,我认为您应该声明所违反的要求,而不是简单地陈述事实。

也就是说,不要说“用户名为空”,而是说“用户名不应该为空”。


关于使用库进行前置条件检查

作为提示,您可以使用旨在促进前置条件检查的众多库之一。 Guava 中的许多代码都使用 <代码>com.google.common.base.Preconditions

在您自己的方法开始时调用简单的静态方法,以验证正确的参数和状态。这允许诸如

之类的构造

 if (count <= 0) {
   throw new IllegalArgumentException("必须为正数:" + count);
 }

更换为更紧凑的

 checkArgument(count > 0, "必须为正数:%s", count);

结构,这里更直接相关的是它具有

  checkNotNull(username, "username should not be null");

/base/Preconditions.html#checkNotNull(T)" rel="nofollow noreferrer">checkNotNull,它允许您简单地 消息明确指出所违反的要求

陈述事实的替代方案更加尴尬:

 // Awkward!
 checkArgument(count > 0, "is negative or zero: %s", count);
 checkNotNull(username, "username is null");

此外,这也可能不太有用,因为客户可能已经意识到这一事实,并且异常并不能帮助他们弄清楚实际的需求。是。


On IllegalArgumentExceptionNullPointerException

虽然原始代码在 null 参数上抛出 IllegalArgumentException,但 Guava 的 Preconditions.checkNotNull< /code> 会抛出 NullPointerException

这符合 API 设定的指南:

NullPointerException< /a>:应用程序应抛出此类的实例以指示 null 对象的其他非法使用。


此外,这里引用了《Effective Java 第二版:第 60 条:支持使用标准异常》:

可以说,所有错误的方法调用都可以归结为非法参数或非法状态,但其他异常通常用于某些类型非法参数和状态。如果调用者在某些禁止使用 null 值的参数中传递 null,则约定将抛出 NullPointerException 而不是 IllegalArgumentException

Since the Exception is thrown due to a failed precondition check, I think rather than simply stating a fact, you should state the requirement that was violated.

That is, instead of saying "username is null", say "username should not be null".


On using libraries for precondition checks

As a tip, you can use one of the many libraries designed to facilitate precondition checks. Many code in Guava uses com.google.common.base.Preconditions

Simple static methods to be called at the start of your own methods to verify correct arguments and state. This allows constructs such as

 if (count <= 0) {
   throw new IllegalArgumentException("must be positive: " + count);
 }

to be replaced with the more compact

 checkArgument(count > 0, "must be positive: %s", count);

More directly relevant here is that it has checkNotNull, which allows you to simply write:

  checkNotNull(username, "username should not be null");

Note how naturally the above code reads, with the detailed message explicitly stating the requirement that was violated.

The alternative of stating facts is more awkward:

 // Awkward!
 checkArgument(count > 0, "is negative or zero: %s", count);
 checkNotNull(username, "username is null");

Moreover, this is also potentially less useful, since the client may already be aware of the fact, and the exception doesn't help them figure out what the actual requirements are.


On IllegalArgumentException vs NullPointerException

While your original code throws IllegalArgumentException on null arguments, Guava's Preconditions.checkNotNull throws NullPointerException instead.

This is in accordance with the guideline set by the API:

NullPointerException: Applications should throw instances of this class to indicate other illegal uses of the null object.

Additionally, here's a quote from Effective Java 2nd Edition: Item 60: Favor the use of standard exceptions:

Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException.

物价感观 2024-09-12 10:21:33

为 null,因为参数仍然为 null。

但是,为什么不简单地抛出一个 NullPointerException 而没有消息呢?

is null, since the argument is still null..

However, why not simply throw a NullPointerException without a message?

ゃ懵逼小萝莉 2024-09-12 10:21:33

我建议说,

  if (userName == null) {
     throw new IllegalArgumentException("username == null");
   }

因为这太致命了,程序员无论如何都必须看看它。在异常消息中引用有问题的代码片段是我能想象的最简洁的事情。

I would suggest saying

  if (userName == null) {
     throw new IllegalArgumentException("username == null");
   }

as this is so fatal that a programmer must look at it anyway. Referring the offending code snippet in the exception message is the concisest thing I can imagine.

痴骨ら 2024-09-12 10:21:33

我更愿意这样写:

public User getUser(String username) {
   if (username.length() == 0) {
       throw new IllegalArgumentException("username is empty");   
   }
   // ...
}

一石二鸟。首先,它检测用户名是空字符串的情况,(为了论证)我假设这是一个错误。其次,如果参数为 null,则尝试分派 length 调用将给出 NullPointerException

根据记录,意外 null 引发的预期异常是 NullPointerException。如果您不使用它的主要原因是 NPE 通常没有消息,请按如下方式编码:

public User getUser(String username){
   if (username == null){
       throw new NullPointerException("username is null");   
   }
   if (username.length() == 0) {
       throw new IllegalArgumentException("username is empty");   
   }
   // ...
}

为什么在这里使用 NPE?因为 NPE 几乎总是表明与其他类型的参数验证错误不同类型的问题;例如,尚未初始化的字段或数组单元格或未正确处理的“可选”值。

最后回答问题:

哪个更好:“is null”“was null”,为什么?

这是一个见仁见智的问题,但我会写“is null”

  • 因为该消息报告的是抛出异常时的状态。
  • 因为这样做是惯例。

I would be inclined to write this:

public User getUser(String username) {
   if (username.length() == 0) {
       throw new IllegalArgumentException("username is empty");   
   }
   // ...
}

This kills two birds with one stone. First, it detects the case where user name is an empty string, which (for the sake of argument) I'm assuming is an error. Second, if the parameter is null attempting to dispatch the length call will give a NullPointerException.

For the record, the expected exception to throw for an unexpected null is NullPointerException. If your main reason for not using it is that NPE's typically don't have a message, code it like this:

public User getUser(String username){
   if (username == null){
       throw new NullPointerException("username is null");   
   }
   if (username.length() == 0) {
       throw new IllegalArgumentException("username is empty");   
   }
   // ...
}

Why use NPE's here? Because NPE's almost always indicate a different kind of problem to a other kinds of argument validation error; e.g. a field or array cell that has not been initialized or an "optional" value that is not being handled properly.

Finally to the question:

What is better : "is null" or "was null", and why?

This is a matter of opinion, but I would write "is null".

  • Because the message is reporting the state when the exception was thrown.
  • Because it is conventional to do it that way.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文