NullPointerException 在哪里抛出?

Where does NullPointerException is thrown?

如果我这样做:

String str = null;
str.toLowerCase();

在这里我将有一个 NullPointerException 并且我想知道将在何处创建异常。 JVM 是否管理此类情况并抛出 NullPointerException 或在编译时在 com.sun.tools.javac.code.Type class?

中对这种情况进行一些检查

我找不到可以向我解释的好文章,而且我也找不到与 OpenJDK javac 源中抛出运行时异常相关的任何内容。任何人都可以根据解释或 link 到 book\article 我可以在其中阅读有关运行时异常创建的信息吗?

回答这部分我想知道异常将在哪里创建?或 Title 本身 NullPointerException 在哪里抛出?

It's is created on the same call Stack, which is the ordered list of methods that had been called to get to the method where the error occurred which then searches for a method that contains a block of code that can handle the exception (i.e exception handler).

图片来源甲骨文。

回答你这部分

或者在编译时对这种情况进行一些检查?

No

因为A 已检查的异常。 必须在您的代码中的某处捕获。如果不是,代码将无法编译。这就是为什么它们被称为检查异常。

开始
String str = null;
str.toLowerCase();

您将能够在不处理任何异常的情况下进行编译,因此不是已检查的异常 (你可以在网上寻找更好的正式定义)。

现在你知道 NullPointerException 不是检查异常。

引用 JSL

The unchecked exception classes (§11.1.1) are exempted from compile-time checking. i.e all the runtime exceptions

当方法检测到这些 RuntimeException 时抛出这些异常,因为 它在虚拟机中遇到错误 runtime