运行时:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
回应是:
true
false
Run Code Online (Sandbox Code Playgroud)
这对我来说相当惊人.我原本以为这会造成编译时错误.
为什么我可以在Java中抛出null,为什么要将它转换为NullPointerException呢?
(实际上,我不知道它是否是"向上",因为我扔的是null)
除了一个非常愚蠢的面试问题(请不要在面试中问这个)我看不出任何理由throw null.也许你想被解雇,但那是......我的意思是,为什么还有人throw null呢?
有趣的事实 IntelliJ IDEA 12告诉我,我的行e instanceof NullPointerException总是假的.这根本不是真的.