如何找到Java项目中所有可能抛出RuntimeException的位置?

发布于 2024-12-17 12:36:49 字数 149 浏览 1 评论 0原文

我有一个 java 项目,我想要源代码中可能抛出 RuntimeException 的位置列表。有没有任何工具或 Eclipse 插件可以做到这一点?

使用 FindBugs 或 PMD 可能会找到 NullPointerExceptions,但我还需要找到其他异常。

I have a java project and I want a list of locations in source code where a RuntimeException might be thrown. Is there any tool or Eclipse plugin to do this?

With FindBugs or PMD the NullPointerExceptions might be located, but I need to locate others as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2024-12-24 12:36:49

NullPointerException、ClassCastException 和 ArithmeticException 是特殊情况,因为它们不需要显式抛出。但是,其他所有内容都必须显式抛出。

搜索 NullPointerException 的可能原因很有用,因为它是一个常见的错误,但是像所有 RuntimeException 一样,最好避免而不是处理它们。搜索所有可能的原因可能会得到一个非常大的结果,但不太可能有用。 (我自己已经完成了这个练习,经过大量的工作并没有取得太多的成果,恕我直言)

您最好使用代码覆盖率并尝试边缘情况值,以确保您拥有良好的测试用例,这些用例可能会触发所有可能的异常。

NullPointerException, ClassCastException and ArithmeticException are a special cases as they do not need to be explicitly thrown. However, everything else has to be thrown explicitly.

Searching for possible causes of NullPointerException is useful as its a common bug, however like all RuntimeException they are best avoided rather than handled. Searching for all possible causes is likely to give you a very large result which is unlikely to be useful. (I have been through this exercise myself and after lots of work didn't achieve much IMHO)

You are better off using code coverage and try edge case values to ensure you have good test cases which are likely to trigger all possible exception instead.

萝莉病 2024-12-24 12:36:49

如果您只检查自己的代码,则可以使用 Checkstyle 来执行此操作。查看编码问题、非法抛出检查。

您可以指定任意数量的异常,默认情况下为:java.lang.Throwable、java.lang.Error、java.lang.RuntimeException

注意:这不会获取上述异常的子类,它只会获取类本身。它也不会检查您无权访问的源代码,因此不会检查运行时库等。

If you're only checking your own code, you can do this with Checkstyle. Look at the Coding Problems, Illegal Throws check.

You can specify any number of Exceptions, by default it is: java.lang.Throwable, java.lang.Error, java.lang.RuntimeException.

Note: This doesn't pick up subclasses of the above exceptions, it only picks up the classes themselves. It also does not check source code to which you don't have access, so runtime libraries won't be checked, for instance.

勿忘心安 2024-12-24 12:36:49

我确实同意其他答案。由于我不知道您想要实现什么,我想对您的问题提供更直接的答案:

不幸的是,我不知道没有工具可以做到这一点。对不起。根据它对您的重要性,您可以自己投入一些工作。

“手动”检查您的代码。

之后 JDK 仍然存在(很可能还有您正在使用的一些库)。您可以从 Oracle 下载 JDK 源代码。您可以再次对新创建的异常实例的出现进行全文搜索。但请注意,即使这项繁琐的工作也不会为您提供所有可能的来源,因为那里缺少一些方法。

I do agree with the other answers. Since I don't know what you want to achieve, I want to offer a more direct answer to your question:

Unfortunately I know no tool that does this. Sorry. Depending on how important it is to you, you could invest some work yourself.

Check your code 'by hand'.

After that the JDK remains (and most likely some libs you are using). You can download the JDKs source code from Oracle. Than you can do a full text search for the occurrences of newly created exception instances again. Note, however, that even this tedious work will not give you all possible sources since some methods are missing there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文