如何使用Intellij Idea的异常断点

时间:2023-01-18 08:59:23

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 Launcher or ClassLoader, but no signs of the actual exception during runtime.

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

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

那么IDEA的异常断点究竟应该如何使用呢?

2 个解决方案

#1


57  

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 exceptions and 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。可能是您已选择捕获任何异常,这意味着它将在启动和类加载期间捕获所有类型的异常。您必须对要捕获的异常做出具体选择,或者只是在捕获异常之前全部检查它们。

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 NullPointerException will be thrown.

我有一个简单的情况,将抛出NullPointerException。

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.

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

如何使用Intellij Idea的异常断点

Enter NullPointerException and press the OK button.

输入NullPointerException并按OK按钮。

如何使用Intellij Idea的异常断点

Make sure that the Any exception is not checked.

确保未选中任何异常。

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

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

如何使用Intellij Idea的异常断点

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

最后,当异常被捕获时,您将看到所有您想要的东西,如调用堆栈和监视窗口。

如何使用Intellij Idea的异常断点

#2


9  

I finally figured out by myself. In the View Breakpoints windows, check Any Exception but 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.

我终于弄明白了。在“查看断点”窗口中,选中“任何异常”,但取消选中“捕获异常”标记。启动程序和类加载器异常由JVM在内部捕获和处理。这样,无需手动指定异常类型即可捕获任何用户异常。

#1


57  

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 exceptions and 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。可能是您已选择捕获任何异常,这意味着它将在启动和类加载期间捕获所有类型的异常。您必须对要捕获的异常做出具体选择,或者只是在捕获异常之前全部检查它们。

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 NullPointerException will be thrown.

我有一个简单的情况,将抛出NullPointerException。

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.

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

如何使用Intellij Idea的异常断点

Enter NullPointerException and press the OK button.

输入NullPointerException并按OK按钮。

如何使用Intellij Idea的异常断点

Make sure that the Any exception is not checked.

确保未选中任何异常。

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

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

如何使用Intellij Idea的异常断点

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

最后,当异常被捕获时,您将看到所有您想要的东西,如调用堆栈和监视窗口。

如何使用Intellij Idea的异常断点

#2


9  

I finally figured out by myself. In the View Breakpoints windows, check Any Exception but 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.

我终于弄明白了。在“查看断点”窗口中,选中“任何异常”,但取消选中“捕获异常”标记。启动程序和类加载器异常由JVM在内部捕获和处理。这样,无需手动指定异常类型即可捕获任何用户异常。