s-day05--Objects里面的requireNonNull方法(了解)
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.exception01;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Objects里面的requireNonNull方法(了解)
|
||||||
|
JDK源码中,它也使用通过throw主动抛出一个异常,而不是让JVM抛出
|
||||||
|
|
||||||
|
总结,该API就是提醒大家,要自己抛出异常,那就用throw关键字
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = null;
|
||||||
|
/*if (str == null) {
|
||||||
|
throw new NullPointerException("str is null");
|
||||||
|
}*/
|
||||||
|
Objects.requireNonNull(str,"str is null,jdk的api实现的");
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user