IDEA中习惯跟踪源码实现逻辑,多次碰到Objects.requireNonNull(T obj)这个方法,改方法主要用于提早判断对象是否为空,以便更早的抛出NPE

平时小组开发中强调程序健壮性,不允许组员的代码中出现明显的NPE,这样多数时候都要写判空逻辑,抛出自定义的异常

我们看下具体的源码:

/**
* Checks that the specified object reference is not {@code null}.
* This method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
解释:检查定义的对象引用不为空。设计该方法主要用来做方法和构造函数中的参数校验
如上,如果直接使用仍然是抛出一个NPE,除了能提早检测和抛出异常,无法直接在业务中使用 继续往下滚动,发现还有这个方法
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.requireNonNull(bar, "bar must not be null");
* this.baz = Objects.requireNonNull(baz, "baz must not be null");
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
} and throws a customized {@link NullPointerException} if it is:定制的空指针异常 and constructors with multiple parameters :多参数的构造函数 这样,下次我们想抛出一个自定义的空指针异常的时候,以前是:
if(obj==null){
  throw new xxxException("该记录不存在xxxx");
}
现在可以更优雅的写Objects.requireNonNull(T obj,"该记录不存在xxxx")

代码实现区别不大,那么为啥不选择优雅^_^
 

如何优雅的使用Objects.requireNonNull(T obj, String message)定制你的NPE异常的更多相关文章

  1. learning java Objects.requireNonNull 当传入参数为null时,该方法返回参数本身

    System.out.println(Objects.hashCode(obj)); System.out.println(Objects.toString(obj)); System.out.pri ...

  2. 【Java】Objects 源码学习

    2017-02-10 by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6387321.html    本篇概要 Objects 与 Object ...

  3. Objects源码解析

    Objects类解析 ​ JDK7新增Objects类介绍(以下程序以1.8来说明) 简介: ​ JDK7里面新增的Objects类,本人学习HashMap源码偶遇此类,所以研究一下,本类将对象常用的 ...

  4. 【jdk源码2】Objects源码学习

    在学习上一个类TreeMap的时候,提到了这个类,这个类是jdk1.7新增的,里面有很多实用的方法.就是一个工具类,熟悉以后,如果里面有已经实现的方法,那么就不要再去实现了,省时省力省测试. 一.简单 ...

  5. 【Java】java.util.Objects 源码学习

    2017-02-10 by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6387321.html    本篇概要 Objects 与 Object ...

  6. java之Objects类

    Objects类概述 在JDK7添加了一个Objects工具类,它提供了一些方法来操作对象,它由一些静态的实用方法组成,这些方法是null-save(空指针安全的)或null-tolerant(容忍空 ...

  7. JDK中工具类的使用

    JDK中内置了很多常用的工具类,且多以“s”结尾,如:集合工具类Collections,数组工具类Arrays,对象工具类Objects,文件工具类Files,路径工具类Paths,数学工具类Math ...

  8. Java8特性---关于Null

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5713941.html ...

  9. jdk1.8新特性应用之Iterable

    我们继续看lambda表达式的应用: public void urlExcuAspect(RpcController controller, Message request, RpcCallback ...

随机推荐

  1. postgresql数字类型

    postgresql的数据类型很多,也可以使用create type命令创建自定义数据类型,但常用的数据类型是以下三种: l  数字数据类型 l  字符串数据类型 l  日期/时间数据类型 数字数据类 ...

  2. 关于BUG管理工具的操作总结。(禅道)

    禅道是第一款国产的优秀开源项目管理软件.先进的管理思想,合理的软件架构,简洁实效的操作,优雅的代码实现,灵活的扩展机制,强大而易用的api 调用机制,多语言支持,多风格支持,搜索功能,统计功能——这一 ...

  3. This inspection detects shadowing names defined in outer scopes.

    错误信息:This inspection detects shadowing names defined in outer scopes. 检查到波浪处的单词已在函数外部定义. 解决:使用global ...

  4. 在Windows上安装部署Cuckoo

    1. Cuckoo使用的第三方工具及库 Yara:http://plusvic.github.io/yara/ Pydeep:https://github.com/kbandla/pydeep Yar ...

  5. 2019牛客多校第二场A-Eddy Walker

    Eddy Walker 题目传送门 解题思路 因为走过所有的点就会停下来,又因为是从0出发的,所以当n>1时,在0停下来的概率为0,其他的为1/(n-1); 代码如下 #include < ...

  6. (转载)理解和使用Promise.all和Promise.race

    声明:本文转载自:https://www.jianshu.com/p/7e60fc1be1b2 一.Pomise.all的使用 Promise.all可以将多个Promise实例包装成一个新的Prom ...

  7. jquery $用法

    //页面刷新时,根据筛选条件中已有的项给下面条件添加样式 window.onload = function() { $("input.query1").each(function( ...

  8. WebStorage篇

    [WebStorage篇] 用户登录状态.计数器或者小游戏等,但是又不希望用到数据库,就可以利用Web Storage技术将数据存储在用户浏览器中. Web Storage是一种将少量数据存储在客户端 ...

  9. 兼容ie8浏览器的准备工作

    一.html5shiv.js和respond.min.js html5shiv:解决ie9以下浏览器对html5新增标签的不识别,并导致CSS不起作用的问题. respond.min:让不支持css3 ...

  10. fabs() abs()

    fabs() 面向实数取绝对值 abs() 返回int