看招聘区的联创面试题想到的这个问题,一开始还以为是因为异常链无法获取到null的信息导致的,后来上网搜了一下,有篇帖子[url]http://www.adarshr.com/papers/npe[/url]解释了这个问题,这其实是Java语言规范里指定的内容,在Java Language Specification第三版$14.18 The throw Statement里面提到了
“A throw statement first evaluates the Expression. If the evaluation of the Expression completes abruptly for some reason, then the throw completes abruptly for that reason. If evaluation of the Expression completes normally, producing a non-null value V, then the throw statement completes abruptly, the reason being a throw with value V. If evaluation of the Expression completes normally, producing a null value, then an instance V' of class NullPointerException is created and thrown instead of null. The throw statement then completes abruptly, the reason being a throw with value V'. ”。说白了就是throw null在运行时会直接变成throw new NullPointerException().
最后
以上就是隐形钻石最近收集整理的关于为什么throw null会抛出NullPointerException?的全部内容,更多相关为什么throw内容请搜索靠谱客的其他文章。
发表评论 取消回复