运行此命令时:
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);
}
}
}
回应是:
true
false
这对我来说相当惊人.我本以为这会导致编译时错误.
Why can I throw null in Java, and why does it upcast it to a NullPointerException?个
(Actually, I don't know if it is an "upcast", given I'm throwing null)
除了一个非常愚蠢的面试问题(请不要在面试中问这个问题),我看不出有什么理由这么做.也许你想被解雇,但那...我的意思是,为什么其他人会 Select throw null
?
Fun fact IntelliJ IDEA 12告诉我,我的第e instanceof NullPointerException
行永远是假的.这根本不是真的.