diff --git a/s-day05/src/com/inmind/exception01/Demo04.java b/s-day05/src/com/inmind/exception01/Demo04.java new file mode 100644 index 0000000..97e3792 --- /dev/null +++ b/s-day05/src/com/inmind/exception01/Demo04.java @@ -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("程序结束"); + } +}