Conditional breakpoints
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A ShopLite import has 1000 rows and the bug is on row 500. A plain breakpoint in the loop means pressing Resume 499 times. You need a breakpoint that stops only where it matters.
Breaking only when a condition is true
A conditional breakpoint pauses only when a boolean expression you attach to it is true at that line, such as i == 499. The expression can use any variable visible on that line.
Example 1 adds up 1000 statement lines, and one of them is a refund. A breakpoint on line 21 with the condition parts[0].equals("REFUND") pauses once, on row 500.
Example 1 · A refund hidden in 1000 lines
import java.util.ArrayList;import java.util.List;
public class UpiBatchDemo { // 1000 lines from the shop's UPI statement; line 500 (index 499) is a refund static List<String> statement() { List<String> lines = new ArrayList<>(); for (int i = 0; i < 1000; i++) { int amount = 40 + (i % 5) * 10; lines.add(i == 499 ? "REFUND|120" : "PAID|" + amount); } return lines; }
public static void main(String[] args) { List<String> lines = statement(); int total = 0; for (int i = 0; i < lines.size(); i++) { String[] parts = lines.get(i).split("\\|"); int amount = Integer.parseInt(parts[1]); total += amount; } System.out.println("Lines: " + lines.size()); System.out.println("Net collected: Rs " + total); }}Lines: 1000 Net collected: Rs 60040
What this code does
- Only one line in 1000 is different, so a plain breakpoint on line 21 would need about 500 Resume presses. The condition
parts[0].equals("REFUND")stops exactly there, withi= 499. - Paused there,
amountis 120 andtotalgrows by it. A refund must be subtracted: the correct net is Rs 59800, not Rs 60040 (Practice 1).
In IntelliJ, right-click the red dot and type the expression in Condition. In VS Code, right-click the gutter, choose Add Conditional Breakpoint..., pick Expression and type it.
The debugger checks the condition every time the line runs, and each check briefly stops the program. In a loop of millions of passes, that can make the run much slower.
Predict the output
When does it pause?
A conditional breakpoint sits on the line marked. Predict where the program pauses.
Round 1 of 2
The condition is i == 499. How many times does the program pause, and with which i?
Hit counts and logging breakpoints that do not suspend
Pass count (IntelliJ) or hit count (VS Code) makes a breakpoint ignore its first passes and pause on a chosen pass, for example the 250th.
In Example 2, Hall A has 250 seats but gets only 249 students. Set a pass count of 250 on line 15 to pause on the 250th seat, or log every seat without pausing at all.
Example 2 · The 250th seat goes to the wrong hall
public class FestSeatsDemo { static final int HALL_A_SEATS = 250;
static String hallFor(int seat) { if (seat < HALL_A_SEATS) { return "Hall A"; } return "Hall B"; }
public static void main(String[] args) { int hallA = 0; int hallB = 0; for (int seat = 1; seat <= 400; seat++) { String hall = hallFor(seat); if (hall.equals("Hall A")) { hallA++; } else { hallB++; } } System.out.println("Hall A: " + hallA + " students (capacity " + HALL_A_SEATS + ")"); System.out.println("Hall B: " + hallB + " students"); }}Hall A: 249 students (capacity 250) Hall B: 151 students
What this code does
- Hall A has 250 seats but only 249 students are sent there. The boundary is the suspect, so pause on the 250th pass with a pass count or hit count of 250.
- A logging breakpoint on line 15 with
"seat " + seat + " -> " + hallwould print 400 lines and never pause, which suits a quick look at a long run. Both show thatseat < HALL_A_SEATSexcludes seat 250 (Practice 2).
A logging breakpoint (VS Code calls it a logpoint) prints a message every time the line runs and does not pause, like a println you never have to write or delete.
- IntelliJ pass count: Run → View Breakpoints, select the breakpoint, tick Pass count and enter 250.
- IntelliJ logging: in the same window untick Suspend and tick Evaluate and log, with an expression such as
"seat " + seat + " -> " + hall. - VS Code: right-click the gutter → Add Conditional Breakpoint..., then choose Hit Count (
250) or Log Message (seat {seat} -> {hall}; expressions go in curly braces).
With a pass count of 250 the program pauses with seat = 250. One Step Over shows hall = "Hall B", although Hall A has 250 seats.
Predict the output
Pass count and logging
Both breakpoints are on the line marked in the loop below.
Round 1 of 2
The pass count is 250. The loop starts at seat = 1. What is seat when the program first pauses?
Exception breakpoints: stop where an exception is thrown
An exception breakpoint pauses the program at the moment a chosen exception is thrown, before any catch block runs. The throwing line and all its variables are still there to inspect.
In Example 3 a catch block counts the bad price but forgets which line it was. An exception breakpoint for NumberFormatException pauses at the throw and shows this simplified view:
Paused by the NumberFormatException breakpoint (frames, newest first; JDK 21 line numbers)forInputString NumberFormatException.java:67parseInt Integer.java:662parseInt Integer.java:778parsePrice PriceImportDemo.java:5 text = "1299.00"main PriceImportDemo.java:20 line = "ACC-AD141,1299.00"The pause starts inside JDK code. Click the first frame from your own class, parsePrice, to see the bad value.
Example 3 · A caught exception hides which line was bad
import java.util.List;
public class PriceImportDemo { static int parsePrice(String text) { return Integer.parseInt(text.strip()); }
public static void main(String[] args) { List<String> csv = List.of( "BK-CLEAN,499", "BK-PRAG,650", "ACC-AD141,1299.00", "BK-ATOMIC,399", "FSH-KURTA,899"); int imported = 0; int skipped = 0; for (String line : csv) { try { String[] parts = line.split(","); parsePrice(parts[parts.length - 1]); imported++; } catch (NumberFormatException e) { skipped++; } } System.out.println("Imported " + imported + " products, skipped " + skipped); }}Imported 4 products, skipped 1
What this code does
- The
catchblock counts the failure but forgets the line and the reason, so the output cannot tell you which product was skipped. - With a NumberFormatException breakpoint (Caught ticked), the debugger stops at the throw. The
parsePriceframe showstext="1299.00":Integer.parseIntdoes not accept decimals.
To add one:
- IntelliJ: Run → View Breakpoints → + → Java Exception Breakpoints, type
NumberFormatException, and tick Caught exception and/or Uncaught exception. - VS Code: in the BREAKPOINTS section of Run and Debug, tick Caught Exceptions and/or Uncaught Exceptions.
Order of execution
Find a hidden exception
The import catches the exception and hides it. Put the steps in the right order.
com.shoplite.*.Method and field-watch breakpoints and their cost
A field watchpoint pauses the program whenever a chosen field is assigned, wherever in the code that happens. It answers the question: who changed this?
In Example 4, stock should end at 33 but ends at 31. A watchpoint on stock pauses at every assignment, and the frames list names the method that made each one.
Example 4 · Who changes the stock? A field watchpoint answers
class Product { final String sku; int stock;
Product(String sku, int stock) { this.sku = sku; this.stock = stock; }
void sell(int quantity) { stock -= quantity; }
void restock(int quantity) { stock += quantity; }
int priceAfterSale(int price, int percentOff) { stock--; // left over from an old "flash sale" counter return price - price * percentOff / 100; }}
public class StockWatchDemo { public static void main(String[] args) { Product book = new Product("BK-CLEAN", 30); book.sell(2); System.out.println("Sale price: Rs " + book.priceAfterSale(499, 10)); book.restock(5); System.out.println("Sale price: Rs " + book.priceAfterSale(499, 20)); System.out.println(book.sku + " stock: " + book.stock + " (expected 33)"); }}Sale price: Rs 450 Sale price: Rs 400 BK-CLEAN stock: 31 (expected 33)
What this code does
- 30 − 2 + 5 should leave 33. A watchpoint on
stock(line 3) pauses at every assignment: line 7 (constructor), line 11 (sell), line 19 (priceAfterSale), line 15 (restock) and line 19 again. - The frames list at each pause names the method, so the two unexpected writes in
priceAfterSalestand out immediately. Removingstock--gives 33 (Practice 4).
A method breakpoint pauses when a method is entered. In IntelliJ, click the gutter on a method's declaration line (a diamond icon) for it, or on a field's declaration line for a watchpoint (an eye icon). VS Code's Java debugger offers Break on Value Change when you right-click a field in VARIABLES.
Method breakpoints make the JVM report every method entry and slow it down badly, so put a line breakpoint on the method's first line instead. Watchpoints cost less, but still slow code that writes the field often.
List field keeps the same list object and someone calls clear() on it, the watchpoint stays silent (Practice 7).Next step
Check your understanding
12 quick questions