Advanced Debugging Techniques in IntelliJ IDEA: Log Evaluation, Field, Exception, and Method Breakpoints
This article introduces four powerful IntelliJ IDEA debugging techniques—log evaluation at breakpoints, field breakpoints, exception breakpoints, and method breakpoints—providing step‑by‑step instructions, code examples, and screenshots to help Java developers debug more efficiently without cluttering their code with temporary prints.
Log Evaluation at Breakpoints
Many developers use print statements for quick debugging, but these often remain in the code and must be removed before committing, increasing review effort and polluting the log tree. IntelliJ IDEA’s Evaluate and Log at Breakpoints feature solves this problem.
Below is a sample Java program used to demonstrate the feature:
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
int count = 0;
for (int i = 0; i < 5; i++) {
if (isInterested(random.nextInt(10))) {
count++;
}
}
System.out.printf("Found %d interested values%n", count);
}
private static boolean isInterested(int i) {
return i % 2 == 0;
}To view the value of i at a specific line without adding a log, place a breakpoint and press Shift + Left‑Click . In the popup, enable Evaluate and log and enter an expression such as "interested" + i . Running the program in Debug mode produces output like:
interested 7
interested 5
interested 1
interested 2
interested 0
Found 2 interested valuesIf multiple breakpoints are used, you can also enable the "Breakpoint hit" message to see where each breakpoint was triggered, yielding logs such as:
Breakpoint reached at top.dayarch.TestDebug.isInterested(TestDebug.java:49)
interested 6
Breakpoint reached at top.dayarch.TestDebug.isInterested(TestDebug.java:49)
interested 0
Breakpoint reached at top.dayarch.TestDebug.isInterested(TestDebug.java:49)
interested 9
Breakpoint reached at top.dayarch.TestDebug.isInterested(TestDebug.java:49)
interested 8
Breakpoint reached at top.dayarch.TestDebug.isInterested(TestDebug.java:49)
interested 1
Found 3 interested values
Disconnected from the target VM, address: '127.0.0.1:0', transport: 'socket'
Process finished with exit codeFor even more detail, enable the "Stack trace" option to view the full call stack at each breakpoint.
Field Breakpoints
When reading source code, it can be hard to track where a particular field’s value changes. IntelliJ IDEA allows you to add a breakpoint on a field so that execution stops whenever the field is accessed or modified.
Click the left mouse button on the field declaration to add a breakpoint (an eye icon appears).
Right‑click the eye icon.
In the dialog, check Field access and Field modification .
If many methods modify the field, you can add a condition in the Condition field to filter when the breakpoint should trigger.
Exception Breakpoints
By default, the debugger stops when an exception is thrown, but you may want to pause before the exception is caught to inspect variable states. Use Exception Breakpoints to stop at the point of exception throw.
After selecting the desired exception type (e.g., NumberFormatException ) in the dialog, run the program in Debug mode. The debugger will halt at the throw point and display the current variable values.
Method Breakpoints
When an interface method has multiple implementations, you can place a breakpoint directly on the interface method (using cmd+F8 or ctrl+F8 ) to stop whenever any implementation is invoked.
Click the left mouse button on the method signature to add a breakpoint (diamond shape).
Right‑click the breakpoint and enable the desired options, optionally adding a Condition .
Running the program in Debug mode will automatically jump into the concrete implementation, allowing you to inspect its behavior.
Summary
With these four debugging techniques—log evaluation at breakpoints, field breakpoints, exception breakpoints, and method breakpoints—Java developers can debug more efficiently, keep their code clean, and gain deeper insight when reading source code.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.