如何优雅的使用Objects.requireNonNull(T obj, String message)定制你的NPE异常
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异常的更多相关文章
- learning java Objects.requireNonNull 当传入参数为null时,该方法返回参数本身
System.out.println(Objects.hashCode(obj)); System.out.println(Objects.toString(obj)); System.out.pri ...
- 【Java】Objects 源码学习
2017-02-10 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6387321.html 本篇概要 Objects 与 Object ...
- Objects源码解析
Objects类解析 JDK7新增Objects类介绍(以下程序以1.8来说明) 简介: JDK7里面新增的Objects类,本人学习HashMap源码偶遇此类,所以研究一下,本类将对象常用的 ...
- 【jdk源码2】Objects源码学习
在学习上一个类TreeMap的时候,提到了这个类,这个类是jdk1.7新增的,里面有很多实用的方法.就是一个工具类,熟悉以后,如果里面有已经实现的方法,那么就不要再去实现了,省时省力省测试. 一.简单 ...
- 【Java】java.util.Objects 源码学习
2017-02-10 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6387321.html 本篇概要 Objects 与 Object ...
- java之Objects类
Objects类概述 在JDK7添加了一个Objects工具类,它提供了一些方法来操作对象,它由一些静态的实用方法组成,这些方法是null-save(空指针安全的)或null-tolerant(容忍空 ...
- JDK中工具类的使用
JDK中内置了很多常用的工具类,且多以“s”结尾,如:集合工具类Collections,数组工具类Arrays,对象工具类Objects,文件工具类Files,路径工具类Paths,数学工具类Math ...
- Java8特性---关于Null
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5713941.html ...
- jdk1.8新特性应用之Iterable
我们继续看lambda表达式的应用: public void urlExcuAspect(RpcController controller, Message request, RpcCallback ...
随机推荐
- 电脑U盘启动制作
1.用老毛桃.大白菜制作U盘驱动时,不要直接默认一键制作.不然安装的系统会植入第三方的软件的.一定要进行个性化设置中取消赞助商.
- (转)ubuntu 下安装mysql5.5.30的过程以及遇到的问题
转:http://blog.chinaunix.net/uid-27103408-id-3280584.html 由于实验需要安装mysql,当然我们可以通过sudo apt-get install ...
- IDEA上传项目到SVN
1.打开IDEA ,上面工具栏选择VCS 选择把项目交给SVN管理 2.选择SVN 3.选择SVN管理后可以看到项目变这个颜色 4.右键项目选择如下 5.点击绿色的+号,选择一个SVN仓库的地址,下面 ...
- leetcode-1053. 交换一次的先前排列
题目描述: 给你一个正整数的数组 A(其中的元素不一定完全不同),请你返回可在 一次交换(交换两数字 A[i]和 A[j] 的位置)后得到的.按字典序排列小于 A 的最大可能排列. 如果无法这么操 ...
- PAT_A1041#Be Unique
Source: PAT A1041 Be Unique (20 分) Description: Being unique is so important to people on Mars that ...
- CDH6.1.0离线安装——笔记
一. 概述 该文档主要记录大数据平台的搭建CDH版的部署过程,以供后续部署环境提供技术参考. 1.1 主流大数据部署方法 目前主流的hadoop平台部署方法主要有以下三种: Apache hadoop ...
- C# WinfForm 控件之dev报表 XtraReport (四) 动态绑定主从关系表
一般的单据都是由主从关系的,比如部门与人员.单据表头与表身.仓库与存货.分类与档案等等 所以主从关系是报表用的最多的 1.准备数据库 简单方便 --主表 create table RdRecord ( ...
- 大型站点图片server架构的演进
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/dinglang_2009/article/details/31450731 在主流的Web站点中,图 ...
- css 给图片添加滤镜效果,透明层毛玻璃效果
我们用的第一个滤镜是sepia(),他会给图片增加一整降饱和度的橙色染色效果 原图 添加sepia滤镜的效果 img{ width:100%; transition: .5s filter; filt ...
- position:relative/static/fixed/absolute定位的区别以及使用场景
absolute是相对于自己最近的父元素来定位的,relative是相对于自己来定位的 relative 不脱离文档流,absolute 脱离文档流.也就是说:relative 的元素尽管表面上看到它 ...