IDEA Debug Tips Collection, you can improve your work efficiency after reading it

About the Author

Table of contents

1. Line breakpoints

2. Method breakpoint

3.Exception breakpoint

4. Field breakpoints

5.Conditional expression


1. Line breakpoints

Line breakpoints are the breakpoints we usually click the mouse next to the line of code. There is nothing to say about this. The key point is that many people don't know that line breakpoints can actually be right-clicked to choose whether to take effect on all calls to the line break or only on the current thread.

I guess many people have encountered it at work. When the front end is connected to our own back end for debugging, we ourselves also need to power off for debugging. If the front end also calls the break point, If you run the code, the front-end friends will get stuck. When bloggers are debugging with the front-end at work, they often hear the front-end say: "Release the breakpoint!" Checking Thread can eliminate this problem.

2. Method breakpoint

Method breakpoints are breakpoints placed on the method signature.

What can method breakpoints be used for:

  1. Review the entire method
  2. Find the implementation class of the interface

1. Review the entire method

I set a method breakpoint on line 20. After clicking debug, a method breakpoint will be automatically set on line 21. This is the characteristic of method breakpoints.

Then you can go down line by line, and finally go to the position of the reverse parenthesis at the end of the method. This is also the most useful point in the actual use of method breakpoints. You can catch it at the end of the method to review the entire The case of method variables:

2. Find the implementation class of the interface

Another particularly cool use of method breakpoints is to find out which implementation method is being called. In actual development, we will encounter situations where an interface has many implementation classes. At this time, it is often not easy to see which implementation is being called. At this time, put a method breakpoint on the interface method, and debug will automatically locate the implementation class:

I first put a breakpoint only on the interface method:

Then debug the previous methd (no need to set any method breakpoints on the method), and it will automatically locate the implementation:

3.Exception breakpoint

What can exception breakpoints be used for:

  • Accurately locate abnormal locations

In actual development, everyone will encounter situations where exceptions are difficult to locate every now and then. Sometimes, many lines of code are tried, and one does not know which line the exception is specifically generated from, so one can only debug line by line. Sometimes, in order to make the program run, the predecessors directly swallowed the exception during try catch instead of throwing it, which ultimately led to the program's running results that were confusing. At this time, exception breakpoints can come in handy. Exception breakpoints can directly capture the line of code that generates the exception, helping us quickly locate the exception.

Click here to enter the breakpoint configuration interface:

Check exception breakpoints and any exception:

If you want to catch a specific type of exception, just click the plus sign to add:

Here we add a null pointer exception.

Then debug again, and the program will stop at the point where a null pointer exception is thrown:

4. Field breakpoints

Field breakpoints are breakpoints placed on the attributes of a class.

What field breakpoints can do:

  • Listen for changes in properties

Prepare an entity class with a fully parameterized structure, get/set methods, and toString methods, and then put a breakpoint on the attribute to be monitored:

Test code:

We debug this code directly, and the breakpoints will display the entire process of field changes in detail. Because the test code first changes the monitored properties when the constructor is constructed, so the breakpoints will go through the entire process starting from the constructor. :

5.Conditional expression

You may encounter such problems in actual development:

When debugging the loop, I only want to capture one of them to check, but because there are too many loops, I click next step crazily in order to wait for the one I need.

IDEA actually prepares conditional expressions for us to accurately grasp the breakpoint when the conditions are met.

For example, if I want to grab the breakpoint where i is an even number in the following loop, I can directly right-click the line breakpoint and configure the expression in condition:

By analogy, very complex conditional judgments can be completed.

Guess you like

Origin blog.csdn.net/Joker_ZJN/article/details/133364144