Using a hit count breakpoint (Intermediate)

Sometimes you want your breakpoint to be triggered when the line is triggered n times. For example, you have a loop and you are interested in inspecting your application only when the counter hits 50, then you use the Hit Count option of the breakpoint. Also sometimes, as I have already mentioned, one line can be called hundreds of times, and if you want to explore a particular situation, you just set your Hit Count to the number at which the breakpoint should be triggered. Thus, the debugger will skip the breakpoint n times and trigger on the count you specified.

Getting ready

Please terminate the debugger if it is still running, remove Printpoint that we have set up in the previous example, and uncomment line 113.

How to do it...

  1. After line 112, please add two more lines:
    andrew.setPosition("programmer");
    andrew.setPosition("employee");

    We need it so that we can call one function several times.

  2. Go to line 31 (if statement of setPosition()) and set a breakpoint.
  3. Right-click on this breakpoint and select Breakpoint Properties….
  4. Check the Hit count option and enter 3 as shown in the following screenshot:
    How to do it...
  5. Click on Ok and run the debugger.

    If you did everything correctly, your program should stop at line 31. Take a look at the Debug view. Your stack shows that setPosition() was called by the main() method on line 114. Line 114 is the third occurrence of the setPosition() function, which means that the two previous occurrences were skipped.

  6. Click on the Resume icon.

Imagine if we do not put the Hit Count breakpoint, then the breakpoint would be triggered for the second and third time. But because we have set Hit count to 3, the breakpoint was triggered only on the third occurrence and simply finished execution.

Now you know how the Hit Count breakpoint works. Please delete lines 113 and 114 that we have added specifically for this example. Also unset the Hit Count breakpoint and terminate the debugger before we move to the next recipe.

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

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