liveBook · Manning

Conditional breakpoint

As explained in Troubleshooting Java, Second Edition

A conditional breakpoint is a debugger breakpoint that pauses execution only when its associated Boolean condition evaluates to true.

A conditional breakpoint pauses program execution only when an associated Boolean expression evaluates to true. It lets you investigate a specific runtime state without stepping through every earlier execution. section 3.1 section 3.5

Use one when a line executes repeatedly but only certain values matter—for example, when investigating why sum sometimes becomes zero. In IntelliJ IDEA, add a breakpoint to the line, open its configuration, and enter a condition such as sum == 0. section 3.1

Finding the zero-sum case

In Decoder.decode, the loop extracts digits, sums them, and adds the result to total. If the summing line executes many times, place a conditional breakpoint there with sum == 0. The debugger skips ordinary iterations and pauses only at the suspicious case, where you can inspect the input, digits, call stack, and surrounding state.

Apply conditional breakpoints as an investigative filter:

  • State the runtime value or situation you need to prove before choosing the condition. section 3.1
  • Use a Boolean expression that can be evaluated at the breakpoint, such as sum == 0. section 3.1
  • Inspect the paused execution path and variables instead of repeatedly stepping through irrelevant cases. section 3.1
  • For lengthy processes, recognize that waiting for a matching breakpoint may still be impractical; consider changing in-scope values when execution pauses. section 3.3
Listing 3.1 Using conditional breakpoints for investigation
public class Decoder {

  public Integer decode(List<String> input) {
    try {
      int total = 0;
      for (String s : input) {
        var digits = new StringDigitExtractor(s).extractDigits();
        var sum = digits.stream().collect(Collectors.summingInt(i -> i));
        total += sum;
      }

      return total;
    } catch (Exception e) {
      return -1;
    }
  }
}

Shows the Java method used to place a conditional breakpoint on a repeatedly executed calculation. section 3.1

How Troubleshooting Java, Second Edition covers it

How deep it goes
Explains Boolean conditions, IntelliJ configuration, repeated execution filtering, and limits for long-running processes.
Examples
Uses Decoder.decode, repeated digit summation, the condition sum == 0, and IntelliJ breakpoint configuration.
What it assumes
Assumes basic debugger navigation and variable inspection while teaching Boolean breakpoint conditions from their practical use.
What you can do after
Diagnose/debug with a debugger by pausing only on executions that satisfy a chosen Boolean condition.
How it connects
Connects conditional breakpoints with debuggers, stepping, stack traces, logging, and runtime variable modification.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.