Sometimes we want our breakpoints to be triggered only under certain conditions. For example, you have a big application with plenty of classes, and when you run it, it gives you unexpected results. If you want to debug such a case, one of the options is to set a breakpoint and track every time certain variables are used, and check the Variables view for the values of these variables. It can take a lot of time, as one method can be called thousands of times. It would be much easier if you could determine the nature of these unexpected results, and trigger a breakpoint only if these results occur. Thus, you should use a conditional breakpoint.
To set up a conditional breakpoint, we will need to make some modifications to our sample program, but first you have to unset the breakpoint from the previous example.
When the breakpoint from the previous example is unset, we will modify our code by performing the following steps:
We will be using the
setPosition()
function (on line 30). Let's comment out theif
statement in this method. Thus we can set any position we like. ThesetPosition()
function will look like the following:30. public void setPosition (String _position) { 31. // if ( Arrays.asList(validPositions).contains(_position)){ 32. position = _position; 33. //} 34. }
Go to the
main()
method and change line 112 to the following:andrew.setPosition("");
It means that we are providing an empty string as a position.
Set a regular breakpoint to the line 32 (
position = _position
).Right-click on the breakpoint and select Breakpoint Properties…. (The last option as shown in the following screenshot.)
In Breakpoint Properties… check the Enable Condition box and in the textbox below type
_position.isEmpty()
.This means that the breakpoint will be triggered only if
_position
is an empty string. Note that, we assign__position
, but not the class variableposition
, because at the time the conditional breakpoint is triggered, the position will not be assigned yet.Click on Ok.
Your breakpoint should now look like this icon. It means that the breakpoint is only triggered when the condition applies.
Now, as we have set up our conditional breakpoint, let's run the application.
Run the debugger by clicking on the icon.
If you did everything correctly, you should see that the application has stopped on line 32 and is waiting for your actions. It stopped because
_position
is empty. If_position
were not empty, it would skip this breakpoint and proceed with executing the application. Thus, a conditional breakpoint allows us to trigger the breakpoints only for the cases when we really want the program to stop executing.Now to make the final change to our application so that we can see the difference, first please terminate by clicking on the icon.
Go to line 112 and pass the word
tester
tosetPosition(String)
. Type the following:andrew.setPosition("tester");
Click on the Debug button again.
This was done to show you that when the position is not empty, the conditional breakpoint is simply skipped.
Now you know how to set up the conditional breakpoint. The condition could be anything you want. It all depends on a particular problem that you are trying to solve.