空检查错误消息为“为空”。或“为空” [英] Null check error message as "is null" or "was null"

查看:235
本文介绍了空检查错误消息为“为空”。或“为空”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java代码中执行空检查,并且您为空值抛出IllegalArgumentExceptions时,您使用什么类型的消息模板?

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.

推荐答案

由于前置条件检查失败而抛出异常,我认为不是简单地陈述一个事实,应该说明违反的要求

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.

也就是说,而不是说username is null,比如说用户名不能为空

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

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

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);
 }

将被更紧凑的替换

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


这里更直接相关的是它有 checkNotNull ,它允许您简单地写:

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.

原始代码抛出 IllegalArgumentException on null 参数,Guava的 Preconditions.checkNotNull 抛出 NullPointerException 而不是。

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

这符合API设定的指导原则:

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


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

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

此外,这是一个来自 Effective Java 2nd Edition的引用:第60项:赞成使用标准例外


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

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.

这篇关于空检查错误消息为“为空”。或“为空”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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