Conditional Breakpoints for Effective Debugging with Eclipse in JAVA

Posted on February 11, 2014 · Posted in Eclipse

If you are working in JAVA and debugging your code in Eclipse and you have case when there are large number of iterations and you are interested to hit breakpoint on line only when certain condition become true then It will  be a better approach to use feature of conditional breakpoint in Eclipse.”

Debugging is the most important activity for developers and nowadays there are many IDEs available with lot of features for debugging. The purpose of this post is to aware you about a very useful feature of Eclipse that can be used when debugging your JAVA code with loops having larger iterator.

In normal breakpoint, you have to repeatedly break and manually check if your condition becomes true. It will be time consuming when you are working with large numbers of iterations. So in that case conditional breakpoints will be helpful. Better understandable by below example.

Suppose you have scenario like below.

Java code for conditional break point

And you need to hit break point on line inside loop only when value of list element is equals to certain value.

To set up conditional breakpoint Right Click on breakpoint.

Conditional berakpoint menu

 

Then select “Breakpoint Properties…” option  and Check ‘Conditional’ .

Conditional Breakpoint eclipse java

Enter your desired condition in text area. Above configuration  will hit breakpoint  only when value of list element is equals to ‘Java’.

Points  to remember:

  • Condition you  set must return Boolean value. Make sure will JAVA allow me to set this condition in IF statement . You can use complex conditions by using && and/or ||.
  • If there is possibility to having null value for object , then better to use null checking to avoid errors , eg. list.get(i) != null && list.get(i).toString.equals(“Java”).
  • You can use auto complete (Ctrl+Space) in the condition text box.

Using Hit count feature:

If you want to hit breakpoint  after line has been executed certain number of times, you can select ’ Hit count ’ option and enter number of iterations you want to skip.

Hit count eclipse conditional nreakpoint

Above Configuration  will  hit breakpoint on line after that line has been executed 10 times.

Also you can use combination of above two types of conditions. So Eclipse will stop breakpoint  only after line has been  executed number of ‘hit counts’ and condition is true.

Note that breakpoint condition is same as normal java code. So take care of scope of variables used in condition  and possibilities of Compile/Run time exceptions and yes, always check your condition is returning Boolean.

I hope this helps!

Regards,

Nikunj Panelia

 

 

 

Share Button