Java Intellij Idea的异常断点如何使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16567136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:27:22  来源:igfitidea点击:

How to use Intellij Idea's exception breakpoints

javadebuggingexceptionintellij-idea

提问by Siyuan Ren

This seems a dumb question, but I can't figure out how to debug exceptions in Intellij Idea. Googling does not help either.

这似乎是一个愚蠢的问题,但我不知道如何在 Intellij Idea 中调试异常。谷歌搜索也无济于事。

I try to create exception breakpoints. When IDEA breaks, I expect it to give me a call stack showing what leads to the method that throws the exception as well as automatic watch window just like Visual Studio's debugging tool. Instead it shows all kinds of garbled message about Launcheror ClassLoader, but no signs of the actual exception during runtime.

我尝试创建异常断点。当 IDEA 中断时,我希望它给我一个调用堆栈,显示导致抛出异常的方法以及自动监视窗口的原因,就像 Visual Studio 的调试工具一样。相反,它显示了关于Launcheror 的各种乱码消息ClassLoader,但在运行时没有实际异常的迹象。

So how exactly is IDEA's exception breakpoint supposed to be used?

那么IDEA的异常断点到底应该怎么用呢?

采纳答案by Siyuan Ren

I finally figured out by myself. In the View Breakpointswindows, check Any Exceptionbut uncheck the mark for Caught exception. The launcher and class loader exceptions are caught and handled internally by the JVM. This way any user exceptions will be caught without manually specifying the type of exception.

我终于自己想通了。在View Breakpoints窗口中,选中Any Exception但取消选中 的标记Caught exception。启动器和类加载器异常由 JVM 在内部捕获和处理。这样就可以在不手动指定异常类型的情况下捕获任何用户异常。

回答by maba

It works really well and I don't really know what kind of problems you have with Launcher or ClassLoader. It could be that you have selected to catch Any exceptionsand this means that it will catch all kinds of exceptions during the startup and class loading as well. You have to make specific choices about what exceptions to catch, or just go through them all until your exception is caught.

它工作得非常好,我真的不知道您在使用 Launcher 或 ClassLoader 时遇到了什么样的问题。可能是您选择了捕获Any exceptions,这意味着它也会在启动和类加载期间捕获各种异常。您必须对要捕获的异常做出具体选择,或者只是遍历所有异常直到捕获到异常。

Anyhow I will show you how I set up a very simple case and you'll see that it is working really well.

无论如何,我将向您展示我如何设置一个非常简单的案例,您会发现它运行得非常好。

I have a simple case where a NullPointerExceptionwill be thrown.

我有一个简单的案例,其中 aNullPointerException将被抛出。

First you'll have to set up the exception breakpoint.

首先,您必须设置异常断点。

Enter the View Breakpoints...window by pressing Ctrl+Shift+F8. Then press the little +sign in the upper left corner.

View Breakpoints...Ctrl+ Shift+进入窗口F8。然后按左上角的小+号。

enter image description here

在此处输入图片说明

Enter NullPointerException and press the OKbutton.

输入 NullPointerException 并按下OK按钮。

enter image description here

在此处输入图片说明

Make sure that the Any exceptionis not checked.

确保Any exception未选中。

Now run the program by right-clicking inside the main method and select Debug 'SomeClass.main()'

现在通过在 main 方法中右键单击运行程序并选择 Debug 'SomeClass.main()'

enter image description here

在此处输入图片说明

And finally watch when the exception is caught and you will have all the things you expected like call stackand watch window.

最后观察异常何时被捕获,您将拥有您期望的所有内容,例如调用堆栈观察窗口

enter image description here

在此处输入图片说明