概述
本文翻译自:Why can I throw null in Java? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Why is “throw null” not creating a compilation error in Java? 为什么“ throw null”不会在Java中创建编译错误? 4 answers 4个答案
When running this: 运行此命令时:
public class WhatTheShoot {
public static void main(String args[]){
try {
throw null;
} catch (Exception e){
System.out.println(e instanceof NullPointerException);
System.out.println(e instanceof FileNotFoundException);
}
}
}
The response is: 响应为:
true
false
Which was fairly stunning for me. 这对我来说真是太棒了。 I would have thought this would net a compile-time error. 我本以为这会带来编译时错误。
Why can I throw null in Java, and why does it upcast it to a NullPointerException? 为什么我可以在Java中抛出null,为什么将其转换为NullPointerException?
(Actually, I don't know if it is an "upcast", given I'm throwing null) (实际上,鉴于我抛出的是null,因此我不知道这是否是“ upcast”)
Aside from a really really stupid interview question (please nobody ask this in an interview) I cannot see any reason to throw null
. 除了一个非常愚蠢的面试问题(请没人在面试中问这个问题)之外,我看不到有任何理由throw null
。 Maybe you want to be fired, but that's... I mean, why else would anyone throw null
? 也许您想被解雇,但这就是...我的意思是,为什么还有人会throw null
呢?
Fun fact IntelliJ IDEA 12 tells me that my line, e instanceof NullPointerException
, will always be false. 有趣的事实 IntelliJ IDEA 12告诉我,我的行e instanceof NullPointerException
始终为false。 Which isn't true at all. 这根本不是真的。
#1楼
参考:https://stackoom.com/question/1BkYk/为什么我可以在Java中抛出null-重复
#2楼
It looks like it's not that null
is treated as a NullPointerException
, but that the act of attempting to throw null
itself throws a NullPointerException
. 看起来不是将null
视为NullPointerException
,而是throw null
自身抛出throw null
的行为将引发NullPointerException
。
In other words, throw
checks that its argument is nonnull, and if it is null, it throws a NullPointerException
. 换句话说, throw
检查其参数是否为null,如果为null,则抛出NullPointerException
。
JLS 14.18 specifies this behavior: JLS 14.18 指定了以下行为:
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. 如果对表达式的求值正常完成,产生一个空值,则将创建并抛出类NullPointerException的实例V',而不是抛出空值。 The throw statement then completes abruptly, the reason being a throw with value V'. 然后,throw语句突然完成,原因是值为V'的throw。
#3楼
Don't know for sure, but I'm guessing that "throw null"; 不确定,但是我猜是“抛出空值”。 does not work, and trying it causes the program to throw an exception, and that exception happens to be (drum roll) NullPointerException... 不工作,并试图导致程序抛出一个异常,并且该异常恰好是(鼓声)NullPointerException异常...
#4楼
It behaves in compliance with the JLS : 它的行为符合JLS的要求 :
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. 如果对表达式的求值正常完成,产生一个空值,则将创建并抛出类NullPointerException的实例V',而不是抛出空值。
#5楼
why does it upcast it to a NullPointerException? 为什么将其转换为NullPointerException?
As per JLS 14.18 : 根据JLS 14.18 :
A throw statement first evaluates the Expression. throw语句首先计算表达式。 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. 如果对表达式的求值正常完成,产生一个非空值V,则throw语句突然完成,原因是带有值V的抛出。 如果对表达式的求值正常完成,产生一个空值,则实例V' NullPointerException类的实例被创建并抛出,而不是null。 The throw statement then completes abruptly, the reason being a throw with value V'. 然后,throw语句突然完成,原因是值为V'的throw。
Why can i throw null in java ? 为什么我可以在Java中抛出null?
You can throw objects of type Throwable
and since null
is a valid reference for Throwable
, compiler allows it. 您可以抛出Throwable
类型的对象,并且由于null
是Throwable
的有效引用,因此编译器允许它。
This is what Neal Gafter says 这就是尼尔·古夫特(Neal Gafter)所说的
Although null is assignable to every reference type, the type of null is not itself a reference type. 尽管null可以分配给每种引用类型,但null类型本身并不是引用类型。 It was our intent that the requirement that the expression in a throw statement be a reference type was to be removed from the third edition of the JLS, but that change never actually made it into the published version. 我们的意图是从JLS的第三版中删除对throw语句中的表达式为引用类型的要求,但是该更改实际上并没有使其成为已发布的版本。 Thus, this is a javac compiler bug which I introduced in SE 5. 因此,这是我在SE 5中引入的javac编译器错误。
#6楼
Thinking about it this way makes it a bit more obvious as to why this works: 通过这种方式进行思考,使其在工作原理上更加明显:
try {
Exception foo = null;
if(false) {
foo = new FileNotFoundException();
} // Oops, forgot to set foo for the true case..
throw foo;
} catch (Exception e){
System.out.println(e instanceof NullPointerException);
System.out.println(e instanceof FileNotFoundException);
}
最后
以上就是悲凉口红为你收集整理的为什么我可以在Java中抛出null? [重复]的全部内容,希望文章能够帮你解决为什么我可以在Java中抛出null? [重复]所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复