Preconditions 是 Google Guava 库中的一个工具类,它提供了一些静态方法,用于进行参数检查。其中,checkNotNull(Object obj) 方法用于检查参数是否为 null,如果是 null,则抛出 IllegalArgumentException 异常。 例如,下面的代码使用 Preconditions 进行 notNull 检查: ``` public void doSomething(String str) { Preconditions.checkNotNull(str, "str must not be null"); // do something with str } ``` 如果 str 参数为 null,则会抛出如下异常: ``` Exception in thread "main" java.lang.IllegalArgumentException: str must not be null at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877) at com.example.MyClass.doSomething(MyClass.java:10) at com.example.Main.main(Main.java:6) ``` 使用 Preconditions 进行参数检查可以使代码更加健壮和可读性更高。