进阶day04-Objects里面的requireNonNull方法(了解)
This commit is contained in:
21
javaSE-day05/src/com/inmind/throw02/ObjectsDemo04.java
Normal file
21
javaSE-day05/src/com/inmind/throw02/ObjectsDemo04.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.inmind.throw02;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/*
|
||||
5.Objects里面的requireNonNull方法(了解)
|
||||
|
||||
JDK源码中,它也是通过throw主动抛出一个异常,而不是让JVM抛出
|
||||
*/
|
||||
public class ObjectsDemo04 {
|
||||
public static void main(String[] args) {
|
||||
String str = null;
|
||||
/*if (str == null) {
|
||||
throw new NullPointerException("str不能为null");
|
||||
}*/
|
||||
|
||||
Objects.requireNonNull(str,"str不能为null");
|
||||
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user