gpt4 book ai didi

java - IntelliJ IDEA 不高亮抛出异常的代码 - 如何打开它?

转载 作者:行者123 更新时间:2023-11-30 06:52:47 25 4
gpt4 key购买 nike

我有一个方法:

private void test() throws NullPointerException {
System.out.println("blabla");
throw new NullPointerException();
}

抛出 NullPointerException 但当我尝试调用此方法时:

test();

IntelliJ IDEA 没有警告我它抛出 NullPointerException。 IntelliJ 应该就此警告我,对吗?

我发现在 Settings -> Editor -> Inspections 中有打开/关闭检查,但我找不到这个检查。

如何开启?

@编辑

好的,我了解到 NullPointerException 是一种 IntelliJ 不会突出显示的异常类型,因为这是一种严重错误。其他异常如 IOException 被突出显示。但是,仍然可以打开突出显示 NullPointerException 吗?

最佳答案

感谢您的编辑,您可以更轻松地解释您所看到的行为。 IntelliJ 标记不子类化的异常 java.lang.RuntimeException因为那是一个实际的编译错误。

让我们看看 Exception 的 JavaDoc :

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

... 以及 RuntimeException 的 JavaDoc:

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

换句话说,您可以在代码中公然抛出 RuntimeException,您不必声明或捕获它,您的编译器会对此非常满意(尽管您的 API 的用户可能不会那么高兴).另一方面,不属于 RuntimeException 子类的异常称为“已检查”异常,必须根据语言规范声明为由您的方法抛出或在可能发生时捕获和处理。

(附言,如果您还没有捕捉到它,NullPointerException 是一个 RuntimeException。因此,它是未经检查的。)

关于java - IntelliJ IDEA 不高亮抛出异常的代码 - 如何打开它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38401821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com