相关疑难解决方法(0)

是否可以通过编程方式抛出NullPointerException?

当有后置条件时,方法的返回值不能为空,可以做什么?

我可以

assert returnValue != null : "Not acceptable null value";
Run Code Online (Sandbox Code Playgroud)

断言可以关闭!

所以可以这样做

if(returnValue==null)
      {
           throw new NullPointerException("return value is null at method AAA");
      }
Run Code Online (Sandbox Code Playgroud)

或者,对于这样的条件,使用用户定义的异常(如NullReturnValueException)会更好吗?

java exception-handling nullpointerexception throw

44
推荐指数
7
解决办法
7万
查看次数