概述
看招聘区的联创面试题想到的这个问题,一开始还以为是因为异常链无法获取到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 null会抛出NullPointerException?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复