我是靠谱客的博主 隐形钻石,这篇文章主要介绍为什么throw null会抛出NullPointerException?,现在分享给大家,希望可以做个参考。

看招聘区的联创面试题想到的这个问题,一开始还以为是因为异常链无法获取到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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部