Using a conditional breakpoint (Intermediate)

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.

Getting ready

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:

  1. We will be using the setPosition() function (on line 30). Let's comment out the if statement in this method. Thus we can set any position we like. The setPosition() function will look like the following:
    30. public void setPosition (String _position) {
    31.   // if ( Arrays.asList(validPositions).contains(_position)){
    32.     position = _position;
    33.   //} 
    34. }
  2. 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.

How to do it...

  1. Set a regular breakpoint to the line 32 (position = _position).

    Tip

    Use Ctrl + L to quickly navigate to the line.

  2. Right-click on the breakpoint and select Breakpoint Properties…. (The last option as shown in the following screenshot.)
    How to do it...
  3. 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 variable position, because at the time the conditional breakpoint is triggered, the position will not be assigned yet.

  4. Click on Ok.

Your breakpoint should now look like this How to do it... 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.

  1. Run the debugger by clicking on the How to do it... 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.
  2. Now to make the final change to our application so that we can see the difference, first please terminate by clicking on the How to do it... icon.
  3. Go to line 112 and pass the word tester to setPosition(String). Type the following:
    andrew.setPosition("tester");
  4. Click on the How to do it... 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.45